I asked a question called 'How to count items in an array that's in an array?' and now I need help on expanding from that question.
How do you count items in two arrays?
My array looks like this:
Array
(
[0] => Array
(
[acf_fc_layout] => irl_today_website_entry
[irl_today_website] => Array
(
[0] => Array
( data removed)
[1] => Array
( data removed )
)
)
[1] => Array
(
[acf_fc_layout] => irl_today_social_entry
[irl_today_social] => Array
(
[0] => Array
( data remove )
[1] => Array
( data remove)
)
)
)
And I use:
<?php $arrays = get_field('irl_today_entry');
$res = array_map(function($x) {
return count($x);
}, array_column($arrays, 'irl_today_website'));?>
to count items in [irl_today_social]. How do I count items in [irl_today_social] and [irl_today_website]?
I tried array_column($arrays, "irl_today_social", "irl_today_website") and it only counted items in [irl_today_social]
array_map() can be fed multiple arrays to work with. The first array "irl_today_social" elements are referenced by $x, the second "irl_today_website" by $y in this case.
Use following:
$res = array_map(function($x, $y) {
$soc = count($x);
$web = count($y);
return ['soc' => $soc, 'web' => $web];
}, array_column($arrays, "irl_today_social"), array_column($arrays, "irl_today_website"));
array_map() will return an array with the count for each - the result sample output:
Array
(
[0] => Array
(
[soc] => 2
[web] => 3
)
)
demo
This is the array:
Array
(
[0] => Array
(
[product_details] => {"5f93f983524def3dca464469d2cf9f3e":{"id":"110","qty":1,"option":"{\"color\":{\"title\":\"Color\",\"value\":null}}","price":1400,"name":"Foot Massage","tax":null,"image":"http:\/\/acme.dev\/uploads\/product_image\/product_110_1_thumb.jpg","coupon":"9","book_date_":"2017-04-19","book_date_name_":"wed","start_timeslot_":"09:00:00","end_timeslot_":"10:00:00","has_already_rescheduled":0,"discount_":"0","rowid":"5f93f983524def3dca464469d2cf9f3e","subtotal":1400}}
)
[1] => Array
(
[product_details] => {"2723d092b63885e0d7c260cc007e8b9d":{"id":"109","qty":1,"option":"{\"color\":{\"title\":\"Color\",\"value\":null}}","price":700,"name":"Body Massage","tax":0,"image":"http:\/\/acme.dev\/uploads\/product_image\/product_109_1_thumb.jpg","coupon":"","book_date_":"2017-04-18","book_date_name_":"tue","start_timeslot_":"09:00:00","end_timeslot_":"10:00:00","has_already_rescheduled":0,"discount_":"0","rowid":"2723d092b63885e0d7c260cc007e8b9d","subtotal":700}}
)
[2] => Array
(
[product_details] => {"a3c65c2974270fd093ee8a9bf8ae7d0b":{"id":"108","qty":1,"option":"{\"color\":{\"title\":\"Color\",\"value\":null}}","price":3000,"name":"Alo","tax":0,"image":"http:\/\/acme.dev\/uploads\/product_image\/default.jpg","coupon":"","book_date_":"2017-04-21","book_date_name_":"fri","start_timeslot_":"10:00:00","end_timeslot_":"12:00:00","has_already_rescheduled":0,"discount_":"0","rowid":"a3c65c2974270fd093ee8a9bf8ae7d0b","subtotal":3000}}
)
[3] => Array
(
[product_details] => {"a3c65c2974270fd093ee8a9bf8ae7d0b":{"id":"108","qty":1,"option":"{\"color\":{\"title\":\"Color\",\"value\":null}}","price":3000,"name":"Alo","tax":0,"image":"http:\/\/acme.dev\/uploads\/product_image\/default.jpg","coupon":"","book_date_":"2017-04-12","book_date_name_":"wed","start_timeslot_":"08:00:00","end_timeslot_":"10:00:00","has_already_rescheduled":0,"discount_":"0","rowid":"a3c65c2974270fd093ee8a9bf8ae7d0b","subtotal":3000}}
)
)
What I need is to create a new simple array containing the values from all the "id" elements.
Hope this simple foreach will be helpful for you.
Solution 1: Try this code snippet here
$result=array();
foreach($array as $value)
{
$array= json_decode($value["product_details"],true);
$result[]=$array[key($array)]["id"];
}
print_r($result);
Here we are using array_column to extract product_details then we are using to array_map to iterate over $personalDetails which contain all the JSON's then we are using to key function which will return first key of the array, and through that key we are accessing, its id.
Solution 2: Try this code snippet here
<?php
ini_set('display_errors', 1);
$array = Array
(
0 => Array
(
"product_details" => '{"5f93f983524def3dca464469d2cf9f3e":{"id":"110","qty":1,"option":"{\"color\":{\"title\":\"Color\",\"value\":null}}","price":1400,"name":"Foot Massage","tax":null,"image":"http:\/\/acme.dev\/uploads\/product_image\/product_110_1_thumb.jpg","coupon":"9","book_date_":"2017-04-19","book_date_name_":"wed","start_timeslot_":"09:00:00","end_timeslot_":"10:00:00","has_already_rescheduled":0,"discount_":"0","rowid":"5f93f983524def3dca464469d2cf9f3e","subtotal":1400}}'
),
1 => Array
(
"product_details" => '{"2723d092b63885e0d7c260cc007e8b9d":{"id":"109","qty":1,"option":"{\"color\":{\"title\":\"Color\",\"value\":null}}","price":700,"name":"Body Massage","tax":0,"image":"http:\/\/acme.dev\/uploads\/product_image\/product_109_1_thumb.jpg","coupon":"","book_date_":"2017-04-18","book_date_name_":"tue","start_timeslot_":"09:00:00","end_timeslot_":"10:00:00","has_already_rescheduled":0,"discount_":"0","rowid":"2723d092b63885e0d7c260cc007e8b9d","subtotal":700}}'
),
2 => Array
(
"product_details" => '{"a3c65c2974270fd093ee8a9bf8ae7d0b":{"id":"108","qty":1,"option":"{\"color\":{\"title\":\"Color\",\"value\":null}}","price":3000,"name":"Alo","tax":0,"image":"http:\/\/acme.dev\/uploads\/product_image\/default.jpg","coupon":"","book_date_":"2017-04-21","book_date_name_":"fri","start_timeslot_":"10:00:00","end_timeslot_":"12:00:00","has_already_rescheduled":0,"discount_":"0","rowid":"a3c65c2974270fd093ee8a9bf8ae7d0b","subtotal":3000}}'
),
3 => Array
(
"product_details" => '{"a3c65c2974270fd093ee8a9bf8ae7d0b":{"id":"108","qty":1,"option":"{\"color\":{\"title\":\"Color\",\"value\":null}}","price":3000,"name":"Alo","tax":0,"image":"http:\/\/acme.dev\/uploads\/product_image\/default.jpg","coupon":"","book_date_":"2017-04-12","book_date_name_":"wed","start_timeslot_":"08:00:00","end_timeslot_":"10:00:00","has_already_rescheduled":0,"discount_":"0","rowid":"a3c65c2974270fd093ee8a9bf8ae7d0b","subtotal":3000}}'
)
);
$personalDetails= array_column($array, "product_details");
$result=array_map(function($value){
$array=json_decode($value,true);
return $array[key($array)]["id"];
}, $personalDetails);
print_r($result);
Output:
Array
(
[0] => 110
[1] => 109
[2] => 108
[3] => 108
)
use array_column and json_decode
$new_one = array_column($array,'product_details');
$new_array=[];
foreach($new_one as $key=>$row)
{
foreach(json_decode($row,true) as $key1=>$row1)
{
$new_array[]=$row1['id'];
}
}
print_r($new_array);
you may use array_map & array_value to achieve this,
here is a quick example, and you need to modify it to be fit with your needs :
$ar = [
0 => ['product_details' => '{"5f93f983524def3dca464469d2cf9f3e":{"id": 3}}'],
1 => ['product_details' => '{"2723d092b63885e0d7c260cc007e8b9d":{"id": 8}}'],
2 => ['product_details' => '{"a3c65c2974270fd093ee8a9bf8ae7d0b":{"id": 5}}'],
3 => ['product_details' => '{"a3c65c2974270fd093ee8a9bf8ae7d0b":{"id": 1}}'],
];
$ar = array_map(function ($value) {
return array_values(json_decode($value['product_details'], true))[0]['id'];
}, $ar);
print_r($ar);
live demo : https://3v4l.org/koXee
Try this code, live demo
print_r(array_column(array_map(function($v){return current(json_decode($v));},array_column($array, 'product_details')), 'id'));
You "product_details" seems to be a JSON string. Loop through your array, decode the JSON and store the "id" in a new array.
I have two array, I would like to merge them without duplicating in "name",
$array1[]= array(name['udi','ari'],id['1','2'])
$array2[]= array(name['udi','ari'],age['22','18'])
result
$arrayresult[]= array(name['udi','ari'],id['1','2'],age['22','18'])
Just simply use array_merge for merging both array as:
Example:
<?php
$array1 = array(
'name'=>array('udi','ari'),
'id'=>array('1','2'),
);
$array2 = array(
'name'=>array('udi','ari'),
'age'=>array('22','18'),
);
$newArr = array_merge($array1,$array2);
echo "<pre>";
print_r($newArr);
?>
Result:
Array
(
[name] => Array
(
[0] => udi
[1] => ari
)
[id] => Array
(
[0] => 1
[1] => 2
)
[age] => Array
(
[0] => 22
[1] => 18
)
)
You can use first $result=array_merge($array1,$array2) and then use $result=array_unique($result) its remove duplicate value.
I think what you're looking for is array_merge:
http://php.net/manual/en/function.array-merge.php
$arrayresult = array_merge($array1,$array2);
should give you:
$arrayresult = array(name('udi','ari'),id('1','2'),age('22','18'))
I have two associative arrays.
array ( [apple]=>1 [banana]=>2 cocunet => 3)
and other array is
array ( [apple]=>2 [banana]=>3 cocunet => 4)
now i want to merge my array like that
array ( [apple]=>1,2 [banana]=>2,3 cocunet => 3,4)
There is no such array in PHP. The thing you want can only be done by creating multidimentional arrays.
$a1 = array( 'apple'=>1,'banana'=>2,'coconut'=> 3);
$a2 = array( 'apple'=>2,'banana'=>3,'coconut'=> 4);
echo "<pre>";
print_r(array_merge_recursive($a1,$a2));
echo "</pre>";
For this you can use the array_merge_recursive() function.
PHPFiddle: http://3v4l.org/5OCKI
If you want the result to be a string then this should work:
foreach( $array_1 as $fruit => $num) {
if(array_key_exists($fruit, $array_2)){ //check if key from array_1 exists in array_2
$final_array[] = array($fruit => $array_1[$fruit].','.$array_2[$fruit]); //concatenate values from shared key
}
}
print_r($final_array) will return:
Array
(
[0] => Array
(
[apple] => 1,2
)
[1] => Array
(
[banana] => 2,3
)
[2] => Array
(
[coconut] => 3,4
)
)
<?php
$array1 = array("apple" => 5, "banana" => 1);
$array2 = array("banana" => 4, "coconut" => 6);
print_r( array_merge_recursive( $array1, $array2 ) );
?>
Returns:
Array ( [apple] => 5 [banana] => Array ( [0] => 1 [1] => 4 ) [coconut] => 6 )
I only used two elements in each of the primary arrays to demonstrate the output prior to having any groups existing, i.e. non-array value.
I need to create an array like the following:
$va_body=array(
"bundles" => array(
"$table.$elem" => array("convertCodesToDisplayText" => true),
"$table.$elem" => array("convertCodesToDisplayText" => true),
)
);
$table is a string that does not change and $elem is extracted from an array.
I got close with the following code, but it ends up with only the last value from $bund, $bund is an array with two values. I guess the array is redeclared in each loop?
$va_body=array(); // declare the array outside the loop
foreach ($bund as $elem ) {
$va_body['bundles'] = array($table.".".$elem=>array("convertCodesToDisplayText" => true));
}
$bund array has two elements "description" and "type_id".
$va_body['bundles'][] // Adding [] doesn't work as it modifies the expected outcome.
print_r($va_body) looks like this:
Array (
[bundles] => Array (
[ca_objects.type_id] => Array (
[convertCodesToDisplayText] => 1
)
)
)
I need it to be:
Array (
[bundles] => Array (
[ca_objects.description] => Array (
[convertCodesToDisplayText] => 1
)
[ca_objects.type_id] => Array (
[convertCodesToDisplayText] => 1
)
)
)
Thanks in advance.
#phpisuber01
Using:
$va_body['bundles'][] = array($table.".".$elem=>array("convertCodesToDisplayText" => true));
print_r($va_body); looks like this:
Array (
[bundles] => Array (
[0] => Array (
[ca_objects.description] => Array (
[convertCodesToDisplayText] => 1
)
)
[1] => Array (
[ca_objects.type_id] => Array (
[convertCodesToDisplayText] => 1
)
)
)
)
And I need it to be like this:
Array (
[bundles] => Array (
[ca_objects.description] => Array (
[convertCodesToDisplayText] => 1
)
[ca_objects.type_id] => Array (
[convertCodesToDisplayText] => 1
)
)
)
Answer by #phpisuber01:
$va_body['bundles'][$table.".".$elem] = array("convertCodesToDisplayText" => true);
Thank you very much!
You need to make an array of arrays. In your loop change the following line:
$va_body['bundles'][$table.".".$elem] = array("convertCodesToDisplayText" => true);
Added [] after $va_body['bundles'].
All this does is keep adding the new bundles into the array. Your original code is overwriting bundles each iteration. That's why you only get the last one.
Updated to get closer to OP's exact needs.
$va_body = array();
$va_body['bundles'] = array();
foreach ($bund AS $elem)
{
$va_body['bundles']["{$table}.{$elem}"] = array("convertCodesToDisplayText" => true);
}