/    Sign up×
Community /Pin to ProfileBookmark

Find "this value" and select parent array

I have this value, doesn’t matter how I obtained it
[B]string(2) “78”[/B]

I have another array, which is something like this

[code=php]
$array[0][id] = 52;
$array[1][id] = 12;
$array[2][id] = 78;
$array[3][id] = 33;
[/code]

I would like to select $array[?] where “78” == $array[?][id]. Without foreach loop. It would result in 4000 heavy comparisons and I’d like to avoid that.

I have ID of product, I’d like to select entire product, AND ALL IT’S ELEMENTS, where one of its elements (id) is equal to product ID I want to select.

So the result would be:

[code=php]
$array[2][id] = 78;
$array[2][some] = “LOL”;
$array[2][more] = “QWE”;
[/code]

So essentially [B]$array[2][/B] would be returned.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmJan 21.2017 — If 'id' is unique, why isn't the array structured differently?
Copy linkTweet thisAlerts:
@HexiconauthorJan 21.2017 — Because the one who created the API hates me. Reconstructing it, would create huge overhead, that I wanted to avoid.
Copy linkTweet thisAlerts:
@HexiconauthorJan 21.2017 — Oh, and also, that's how SQL database sort them. It's an SQL result. First data, second data, third data, not sorted by ID.
Copy linkTweet thisAlerts:
@NogDogJan 21.2017 — You could do:
[code=php]
$flattened = array_column($array, 'id');
$id = array_search(78, $flattened);
[/code]
Copy linkTweet thisAlerts:
@HexiconauthorJan 22.2017 — Can't seem to be able to copy entire script without Chinese characters.

Here's reproduced "issue".

[code=php]
<?php

$example["matches"][0]["matchId"] = 3024554258;
$example["matches"][0]["champion"] = 121;
$example["matches"][0]["role"] = "none";

$dict[5]; // they do exist
$dict[6]; // they do exist
$dict[7]["id"] = 121;
$dict[7]["name"] = "Hexicon";
$dict[7]["title"] = 'u w0t m8?';
$dict[8]; // they do exist

$i = 0;

foreach($example["matches"] as $match) {
$i++;

$result[$i]["champion"] = $match["champion"];

$named = array_search($result[$i]["champion"], array_column($dict, "id"));

var_dump($result[$i]["champion"]);

echo $result[$i]["champion"] . " is now named: " . $named;
// expected: 121 is now named Hexicon
// $result[$i]["champion"] = int(121)
// $named = string(7)
}

?>
[/code]


That doesn't work.
Copy linkTweet thisAlerts:
@ginerjmJan 22.2017 — You're working with an uncooperative associate. Hmmm.... Don't suppose you could write your own query to produce what you need directly instead of manipulating things in multiple complex arrays? Doing things with sql engines is pretty fast - probably much faster than array processing.
Copy linkTweet thisAlerts:
@HexiconauthorJan 22.2017 — It's how SQL produces them.

"SELECT * FROM anything"

Will always return

[0][id]

[0][name]

[1][id]

[1][name]

As it sorts these data row by row. Which is what it outputs.
Copy linkTweet thisAlerts:
@ginerjmJan 22.2017 — Actually it will produce whatever you want.

Looking at all the items you are putting into your 3D arrays I'm thinking that you have multiple queries(?) producing many different attributes from perhaps multiple tables. A well-written query (select * is NOT one) could produce the structure you need in a single record (albeit an array) that you could use very sensibly perhaps.

Try adding a 'return feature' to your fetch so you get only one array instead of the default 'BOTH' to make your life a bit easier.
×

Success!

Help @Hexicon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...