I need to convert array_1 to array_2 with a PHP function. I tried many things but nothing works. I hope someone can help me out here. I think I need an each function or something to loop through the comma separated array and convert it into the array_2.
$array_1 = array (
0 => '6801,6800,7310,6795',
);
$array_2 = array (
0 =>
array (
0 => '6801',
1 => '6800',
2 => '7310',
3 => '6795',
),
);
Here a solution
<?php
$array_1 = array (
0 => '6801,6800,7310,6795',
);
$array_2 = array();
foreach ($array_1 as $value) {
array_push($array_2 , explode(",",$value));
}
print_r($array_2);
?>
The output that i got
Array ( [0] => Array ( [0] => 6801 [1] => 6800 [2] => 7310 [3] => 6795 ) )
Use PHP explode function. https://www.php.net/manual/de/function.explode.php
$newArray[] = explode(",", $array_1[0]);
// output
Array
(
[0] => Array
(
[0] => 6801
[1] => 6800
[2] => 7310
[3] => 6795
)
)
Just create a new array with a value returned by the explode function. Reset always returns the first value of the array regardless of the key.
$array_1 = array (0 => '6801,6800,7310,6795');
$newArray = [explode(",", reset($array_1))];
I am new to PHP an needed little help. It may be easy for some but giving me a tough time.
I have an array
Array ( [0] => page-18 [1] => page-20 )
Which I would like to explode further by '-':
$mainStringBrk = array('page-18', 'page-20');
$finalArray = array();
foreach($mainStringBrk as $bString){
$mainStringBrkBrk = explode('-', $bString);
$finalArray[$mainStringBrkBrk[0]] = $mainStringBrkBrk[1];
}
echo '<pre>'; print_r($finalArray);
When I do, it outputs only the last key and value of array.
Array ( page => 20 )
My desired output is:
Array ( page => 18, page => 20 )
I am wondering if anyone can guide me in right direction.
You can't achieve the result you want as it is not possible to have an array with identical keys; this is why you only have one result in your output. You could change your output structure to a 2-dimensional array to work around this e.g.
$mainStringBrk = array('page-18', 'page-20');
$finalArray = array();
foreach($mainStringBrk as $bString){
$mainStringBrkBrk = explode('-', $bString);
$finalArray[$mainStringBrkBrk[0]][] = $mainStringBrkBrk[1];
}
print_r($finalArray);
Output:
Array
(
[page] => Array
(
[0] => 18
[1] => 20
)
)
Or you can adopt this structure if it is better suited to your needs:
$finalArray = array();
foreach($mainStringBrk as $bString){
$mainStringBrkBrk = explode('-', $bString);
$finalArray[] = array($mainStringBrkBrk[0] => $mainStringBrkBrk[1]);
}
print_r($finalArray);
Output:
Array
(
[0] => Array
(
[page] => 18
)
[1] => Array
(
[page] => 20
)
)
Demo on 3v4l.org
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 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 would like to convert / form the following arrays as example:
Array ( [product_category] => for-women ) Array ( [brand] => 7-diamonds ) Array ( [size] => 12 ) Array ( [color] => 882536 )
Into one array that just merges each array pair and put them altogether :
Array ( [product_category] => for-women [brand] => 7-diamonds [size] => 12 [color] => 882536 )
I tried array_merge and it didn't work. The array out put in my code is from $_SESSION which returns an array (a pair key=> value) like this:
foreach($_SESSION as $k => $v) {
if (strstr($k, 'saved_query_') == true) {
$saved = array_merge($v);
}
}
So I get each array by looping through session which has a query, the result is array pair, I want to combine all pairs found (Do not know how to use array_merge in that case).
I tried array_combine and array_merge they do not seem like the functions I need based on php manual:
array_combine — Creates an array by using one array for keys and another for its values
Which I do not want to do, I just want to copy/move small arrays in one array, without changing any pairing/key/value.
You can try using array_merge
$array0 = Array ( "product_category" => "for-women" );
$array1 = Array ( "brand" => "7-diamonds" ) ;
$array2 = Array ( "size" => "12" ) ;
$array3 = Array ( "color" => "882536" );
$array = array_merge($array0,$array1,$array2,$array3);
print_r($array);
Output
Array ( [product_category] => for-women [brand] => 7-diamonds [size] => 12 [color] => 882536 )
See Demo
* ----- Update ----- *
If you are looking through a session
$_SESSION = Array();
$_SESSION[0] = Array("product_category" => "for-women");
$_SESSION[1] = Array("brand" => "7-diamonds");
$_SESSION[2] = Array("size" => "12");
$_SESSION[3] = Array("color" => "882536");
$final = array();
foreach ( $_SESSION as $key => $value ) {
$final = array_merge($final, $value);
}
print_r($final);
Use array_merge_recursive() :
$result = array_merge_recursive($ar1, $ar2 [, array $...]);
Example: http://codepad.viper-7.com/Yr0LTb
Use array_merge instead.
$ret = array_merge($arr1, $arr2, $arr3);
With your code, you should do:
$saved = array_merge($saved, $v);
You should have a look at the array_merge() function in PHP: http://php.net/manual/en/function.array-merge.php
Simply use as follows:
$array1 = Array ( [product_category] => for-women );
$array2 = Array ( [brand] => 7-diamonds );
$array3 = Array ( [size] => 12 );
$array4 = Array ( [color] => 882536 );
$combined = array_merge($array1, $array2, $array3, $array4);