I have a multidimensional array that loks lik this:
Array
(
[0] => Array
(
[name] => >chr1:2198584545754_genome_1000+
[score] => 511
[hit] => 50
)
[1] => Array
(
[name] => >chr2:2198581212154_genome_1000+
[score] => 620
[hit] => 80
)
[2] => Array
(
[name] => >chr3:2115151215754_genome_1000+
[score] => 666
[hit] => 90
)
[3] => Array
(
[name] => >chr4:2198584545754_genome_1000+
[score] => 750
[hit] => 50
)
[4] => Array
(
[name] => >chr5:1218455145754_genome_1000+
[score] => 800
[hit] => 100
)
[5] => Array
(
[name] => >chr6:1231354645454_genome_1000+
[score] => 850
[hit] => 110
)
[6] => Array
(
[name] => >chr7:1231213211134_genome_1000+
[score] => 900
[hit] => 120
)
)
I have a foreach loop which will loop through each letter of a random sequence and use the index to give each letter a number value.
If the value of ['hit'] matches the index value of the random sequence
i want to insert a function.
I cannot figure this out. I think my problem is in callng each value of ['hit'] and comparing with index. Does anyone know how to do this ?
thanks
To put DaveRandom's comment into an answer (with minor amends):
foreach ($outerArray as $index => $innerArray)
{
if($innerArray['hit'] === $index)
{
doSomething();
}
}
#DaveRandom - feel free to delete or re-post this as your own answer if I'm posting out of turn here...
foreach ($array as $key) {
if ($key['hit'] == $index)
{
// you function or logic here
}
}
Related
I am new to PHP and Arrays, I am trying to get the values from an array. But no matter how I'm trying to do it, I can't get the value. What am I doing wrong?
The Array:
Array ( [playerinfo] => Array ( [rank] => Godfather [cash] => € 8,520,530 [weapon] => M-16 (29000) [health] => Array ( [width] => 100 [color] => green ) [wealth] => Too rich to be true [protection] => Bulletproof Humvee [plf] => Huge [plane] => Concorde [crew] => None [pbf] => Large [ship] => None ) [character] => Array ( [crime] => Array ( [0] => 120 [1] => 69 ) [gta] => Array ( [0] => 400 [1] => 70 ) [drugs] => Array ( [0] => 120 [1] => 2528 ) [airport] => Array ( [0] => 2700 [1] => 2529 ) [oc] => Array ( [0] => 86400 [1] => 1442364 ) [tr] => Array ( [0] => 10800 [1] => 1640016011 ) [plf] => Array ( [0] => 7200 [1] => 6712 ) [kill] => Array ( [0] => 3600 [1] => 1640019611 ) ) )
The way I tried to get the info:
$AccData = json_decode($MobinfoString, true);
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['cash'].'<br/>';
foreach ($AccData as $playerinfo) {
echo $playerinfo['playerinfo']['rank'].'<br/>';
echo $playerinfo['character']['gta'].'<br/>';
}
EDIT:
The json string
{"playerinfo":{"rank":"Boss","cash":"€ 5,923,712","weapon":"M-16 (4500)","health":{"width":"100","color":"green"},"wealth":"Too rich to be true","protection":"Bulletproof Humvee","plf":"Huge","plane":"Concorde","crew":"None","pbf":"Large","ship":"None"},"character":{"crime":[120,122],"gta":[400,369],"drugs":[120,2582],"airport":[2700,2582],"oc":[86400,1640020450],"tr":[10800,1640016850],"plf":[7200,3935],"kill":[3600,1640020450]}}
Anyone knows how to do this ? For example I need the Concorde from plane in a variable and the time values from gta in a variable. And some more from this string.
So your first Part is okay and you rank you can just display like the first part as well
$AccData = json_decode($MobinfoString, true);
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['cash'].'<br/>';
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['plane'].'<br/>';
echo $AccData['character']['gta'][0].'<br/>';
but the character is on the same level as playerinfo so you need to access it from AccData. also gta is an array like health, so you have to specify which value you want to show, the first so 0 or second which is 1
My array look like this :
[cart_seller] => Array
(
[3] => Array
(
[หมวด1เลือก1 หมวด2เลือก1 1705] => Array
(
)
)
[4] => Array
(
[# 801] => Array
(
)
)
)
[cart_product] => Array
(
[หมวด1เลือก1 หมวด2เลือก1 1705] => Array
(
[id] => 1705
[name] => ทดสอบสินค้า
[image] => p1534937865-VASAVAT LAB N MEDIA LOGO W.png
[price] => 1111
[option] => หมวด1เลือก1 หมวด2เลือก1
[amount] => 1
)
[# 801] => Array
(
[id] => 801
[name] => โบว์แพรแถบ ร.9 ชนมพรรษา 84 พรรษา ปีพุทธศักราช 2554
[image] => p1498062217-ส.jpg
[price] => 90
[option] =>
[amount] => 1
)
)
I want unset '# 801' in cart_seller and cart_product
in cart_product use unset($cart['cart_product'][# 801]);
but in cart_seller it in array [4] what can i do without reference value (4) ?
exapmle unset($cart['cart_seller'][xxxx][# 801]);
Just loop cart seller array til you find #801.
foreach($cart['cart_seller'] as $key => $c){
if(array_key_exists("#801",$c)){
unset($cart['cart_seller'][$key]['#801']);
}
}
I have array that contain an score and id that calculated from other function
And I have user info that retried from DB.
In Both array ID's are the same
how can I push them to One array?
Score Array
Array
(
[0] => Array
(
[id] => 85
[total_cnt] => 2006
)
[1] => Array
(
[id] => 86
[total_cnt] => 1014
)
[2] => Array
(
[id] => 92
[total_cnt] => 6
)
[3] => Array
(
[id] => 93
[total_cnt] => 6
)
)
user info
Array
(
[0] => Array
(
[id] => 52
[user_phone] => 00000000
[user_email] => test#yahoo.com
[user_name] => yahoo
[user_picture] =>FG6K7Z3XTc.Pic.jpg
[user_post_hour] => 24
[user_is_block] => 1
[user_reg_date] => 2017-05-16 13:52:35
)
[1] => Array
(
[id] => 78
[user_phone] => 000000001
[user_email] => google#gmail.com
[user_name] => google
[user_picture] =>XqWKSDVci.Pic.jpg
[user_post_hour] => 24
[user_is_block] => 0
[user_reg_date] => 2017-05-16 13:52:35
)
)
My Desire output
Array
(
[0] => Array
(
[id] => 86 <--Same ID in both arrays
[user_phone] => 00000000
[user_email] => test#yahoo.com
[user_name] => yahoo
[user_picture] =>FG6K7Z3XTc.Pic.jpg
[user_post_hour] => 24
[user_is_block] => 1
[user_reg_date] => 2017-05-16 13:52:35
[total_cnt] => 1014 <-- first array field added
)
I want an optimized code and I won't use loop for to do this
Thanks for your help
Use PHP's built-in function array_merge. Use the official PHP documentation for additional guidance # http://php.net/manual/en/function.array-merge.php
Update:
A much better approach seems to be "array_column":
$cnts = array_column($scores, 'total_cnt', 'id');
foreach ($userInfo as $key => $item) {
$userInfo[$key]['total_cnt'] = $cnts[$item['id']];
}
I made some "naive" benchmark tests using microtime() and test data like your arrays:
Execution times:
10000 items in both arrays:
array_column 0.005s vs 0.85s foreach
20000 items in both arrays:
array_column 0.011s vs 18s foreach
Original answer:
You can also use foreach loops like this:
foreach ($userInfo as $userKey => $item) {
foreach ($scores as $scoreKey => $score) {
if ($score['id'] == $item['id']) {
$userInfo[$userKey]['total_cnt'] = $score['total_cnt'];
unset($scores[$scoreKey]);
break;
}
}
}
The unset within the second loop "removes" the processed score from the $scores array to reduce the number of iteration cycles in the next run. Please note that the $scores array will be empty afterwards, maybe create a copy of it and work with that.
Can you please explain how to change one array value to second value.
Please find below example.
First array
Array
(
[20239802] => one test
[20239801] => two testttttt
)
Second array
Array (
[content] => Array (
[0] => Array (
[content_pack_id] => 10002
[content_pack_name] => 100 Days Of Love-FLA
[image_path] => Array ( [0] => pack_image_10002_width. )
[content_image_path] => Array ( [imgjpeg80] => http://content.jpg )
[content_id] => 20239802
[track] => Lede Inthati Santhosham
[duration] => 0
)
[1] => Array (
[content_pack_id] => 10003
[content_pack_name] => 1001 fdfdf
[image_path] => Array ( [0] => pack_image_10002_width. )
[content_image_path] => Array ( [imgjpeg80] => http://content.jpg )
[content_id] => 20239801
[track] => Lede Inthati Santhosham
[duration] => 0
)
)
[autoshuffle_pack] => no
)
We need to replace [track] value in second array if match first array [20239802] with second array [content_id]
Need out put:-
Array
(
[content] => Array
(
[0] => Array
(
[content_pack_id] => 10002
[content_pack_name] => 100 Days Of Love-FLA
[image_path] => Array
(
[0] => pack_image_10002_width.
)
[content_image_path] => Array
(
[imgjpeg80] => http://content.jpg
)
[content_id] => 20239802
[track] => one test
[duration] => 0
)
[1] => Array
(
[content_pack_id] => 10003
[content_pack_name] => 1001 fdfdf
[image_path] => Array
(
[0] => pack_image_10002_width.
)
[content_image_path] => Array
(
[imgjpeg80] => http://content.jpg
)
[content_id] => 20239801
[track] => two testttttt
[duration] => 0
)
)
[autoshuffle_pack] => no
)
Check [track] value change in my need out put
as per depend first array.
Array
(
[20239802] => one test
[20239801] => two testttttt
)
with second array
[content_id] => 20239801
[track] => two testttttt
Is this you want to do :
foreach($second_array as $key => $second_row) {
$content_id = $second_row['content_id'];
if(isset($first_array[$content_id]) && $first_array[$content_id] != '') {
$second_array['track'] = $first_array[$content_id];
}
}
You can do you own function.
Foreach records in your first array:
Loop though the second and search for key == content_id
If it matched, set the value of array2[$index][track] to array1[key]
If not just continue
You can also use array_search() function to find the match faster, for I'll let you take a look at the PHP documentation
I have an array $products that looks like this
Array
(
[services] => Array
(
[0] => Array
(
[id] => 1
[icon] => bus.png
[name] => Web Development
[cost] => 500
)
[1] => Array
(
[id] => 4
[icon] => icon.png
[name] => Icon design
[cost] => 300
)
)
)
I am trying to delete the part of array that matches [id] => 1 and for this I am using the following code
$key = array_search('1', $products);
unset($products['services'][$key]);
However it is not working and I am not getting any error either.
What am i doing wrong?
This should work for you:
$key = array_search('1', $products["services"]);
//^^^^^^^^^^^^ See here i search in this array
unset($products['services'][$key]);
print_r($products);
Output:
Array ( [services] => Array ( [1] => Array ( [id] => 4 [icon] => icon.png [name] => Icon design [cost] => 300 ) ) )
And if you want to reindex the array, so that it starts again with 0 you can do this:
$products["services"] = array_values($products["services"]);
Then you get the output:
Array ( [services] => Array ( [0] => Array ( [id] => 4 [icon] => icon.png [name] => Icon design [cost] => 300 ) ) )
//^^^ See here starts again with 0
This will loop through $products['services'] and delete the array whose 'id' key has value 1. array_values just re-indexes the array from 0 again.
foreach($products['services'] as $key => $service)
{
if($product['id'] == 1)
{
unset($products['services'][$key]);
array_values($products['services']);
break;
}
}