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'))
my output is
Array ( [bid] => 1 [bookiing_date] => 2014-11-19 )
Array ( [bid] => 2 [bookiing_date] => 2014-11-15 )
Array ( [bid] => 3 [bookiing_date] => 2015-01-10 )
Array ( [bid] => 4 [bookiing_date] => 2015-01-27 )
but i want to convert in this form..
$date = ['2015-01-10','2015-01-27','2014-11-19'];
Please any one help me
What you've written above doesn't look too much like PHP, but something like this should do the trick
$single_dimensional_array = array();
foreach($multi_dimensional_array as $array_item) {
if(isset($array_item['bookiing_date'])) {
$single_dimensional_array[] = $array_item['bookiing_date'];
}
}
echo(var_export($single_dimensional_array), true);
Btw, the $single_dimensional_array[] = syntax above simply pushes the value on the right side of the equal sign onto the end of the array on the left side of the equal sign.
There is a very simple approach to get that... I think you did not tried anyway..here is the basic and simple answer ...
$sing_arr = array();
$multi_arr = array(array ( "bid"=> 1, "bookiing_date"=> "2014-11-19" ) ,
array ( "bid"=> 2, "bookiing_date"=> "2014-11-15" ) ,
array ( "bid"=> 3, "bookiing_date"=> "2015-01-10" ) ,
array ( "bid"=> 4 ,"bookiing_date"=> "2015-01-27" )
);
foreach($multi_arr as $key=>$val)
{
array_push($sing_arr,"'".$val["bookiing_date"]."'");
}
echo var_dump($sing_arr);
You Can Loop Through the array using foreach and store the date in another array:
Code:
<?php
$array1=array(
Array ( "bid" => 1 ,"bookiing_date" => "2014-11-19" ),
Array ( "bid" => 2 ,"bookiing_date" => "2014-11-15" ),
Array ( "bid" => 3 ,"bookiing_date" => "2015-01-10" ),
Array ( "bid" => 4 ,"bookiing_date" => "2015-01-27" )
);
// print_r($array1);
$date=array();
foreach ($array1 as $temparray){
$date[]=$temparray["bookiing_date"];
}
print_r($date);
Output:
Array
(
[0] => 2014-11-19
[1] => 2014-11-15
[2] => 2015-01-10
[3] => 2015-01-27
)
Use this
$array = array(array('bid' => 1, 'bookiing_date' => 2014 - 11 - 19),
array('bid' => 2, 'bookiing_date' => 2014 - 11 - 15),
array('bid' => 3, 'bookiing_date' => 2015 - 01 - 10),
array('bid' => 4, 'bookiing_date' => 2015 - 01 - 27));
$data = array();
foreach ($array as $array_item){
if(isset($array_item['bookiing_date'])) {
$data[] = $array_item['bookiing_date'];
}
}
print_r($data);
if you are using PHP 5.5+, use array_column()
$data = array_column($array, 'bookiing_date');
print_r($data);
I have two multi-dimensional array I want to take only those array whose
key values are different from the first array
Here is my two array:
$array1 = Array
(
[0] => Array
(
[id] => 1
[serial] => k-0001
[u_rec_id] => 1
[employer_office] => uouuououou
[job_type] => ouuou
[job_title] => u
[job_appointment_date] => 2013-07-15
[job_duration] => ouu
)
[1] => Array
(
[id] => 2
[serial] => k-0001
[u_rec_id] => 1
[employer_office] => DDC
[job_type] => Manger
[job_title] => Manager
[job_appointment_date] => 2013-07-17
[job_duration] => one year
)
)
and this is my second array
$array2 = Array
(
[0] => Array
(
[id] => 1
[serial] => k-0001
[u_rec_id] => 1
[employer_office] => uouuououou
[job_type] => ouuou
[job_title] => u
[job_appointment_date] => 2013-07-15
[job_duration] => ouu
)
[1] => Array
(
[id] => 2
[serial] => k-0001
[u_rec_id] => 1
[employer_office] => ouo
[job_type] => uououo
[job_title] => udds
[job_appointment_date] => 2013-07-17
[job_duration] => uo
)
);
I tried array_diff and array_diff_assoc it also not worked for me
i get this error
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: history/home.php
Line Number: 729
Something like this should get you there, depending on what exactly you want:
$diff = array_udiff($array1, $array2, function (array $a, array $b) {
return (int)array_diff($a, $b);
});
Adjust the comparison function to compare what exactly you want to compare.
http://php.net/array_udiff
foreach, array_unique and possibly array_udiff should help you here.
PHP Manual:
Unique Arrays
Data Comparison
For Each
For a simple array:
$result = array_unique($array);
In your case there's a function from PHP Manual for this:
<?php
function specified_array_unique($array, $value)
{
$count = 0;
foreach($array as $array_key => $array_value)
{
if ( ($count > 0) && ($array_value == $value) )
{
unset($array[$array_key]);
}
if ($array_value == $value) $count++;
}
return array_filter($array);
}
?>
There's been a post that is similar to what you're asking;
Stack Overflow - array_udiff
I have an array like the following . . .
Array
(
[code] => BILL
[assets] => Array
(
[en] => Array
(
[labels] => Array
(
[datestamp] => April 30, 2013
)
[data] => Array
(
[Equity] => 88.83000000000
[Global] => 10.84000000000
[Other] => 0.33099095766
)
)
)
)
I have run the array_map function on this array to remove the [en] array
$en = array_map(function ($e){ return $e['en']; } , $en );
note how the resulting array has truncated the value for [code] from BILL to B
Array
(
[code] => B
[assets] => Array
(
[labels] => Array
(
[datestamp] => April 30, 2013
)
[data] => Array
(
[Equity] => 88.83000000000
[Global] => 10.84000000000
[Other] => 0.33099095766
)
)
)
Any tips on how to avoid that from happening. It is removing the array with the key [en] as required, but I don't want the value for [code] to be truncated.
Thanks.
You could perform type checking:
$en = array_map(function ($e){
if (is_array($e)) {
return $e['en'];
} else {
return $e;
}
} , $en );
Although it might suffice to do just this:
$en['assets'] = $en['assets']['en'];
Hello instead of passing entire array you mentioned as argument, you can pass the assets part of array as argument to array_map function :
$en_new = array_map(function ($e){ return $e['en']; } , $en['assets'] );
and then append the code part:
$en_new['code'] = $en['code'];