Below is my array which i have printed:-
I want only the product_image from the array in loop
Array
(
[0] => Array
(
[product_option_id] => 247
[product_id] => 66
[product_option_value] => Array
(
[0] => Array
(
[product_option_value_id] => 42
[color_product_id] => 54
[name] => Pink
[product_image] => catalog/demo/teddy/03.jpg
[image] => http://192.168.15.9/Kids_stores/image/cache/catalog/axalta-ral-3015-light-pink-polyester-30-matt-powder-coating-20kg-box--1447-p-50x50.jpg
[price] =>
[price_prefix] => +
)
[1] => Array
(
[product_option_value_id] => 41
[color_product_id] => 67
[name] => Light Brown
[product_image] => catalog/Teddies/12-Baby-teddy/05.jpg
[image] => http://192.168.15.9/Kids_stores/image/cache/catalog/option-color/light_brown-50x50.jpg
[price] =>
[price_prefix] => +
)
[2] => Array
(
[product_option_value_id] => 43
[color_product_id] => 68
[name] => Cream
[product_image] => catalog/Teddies/12-Baby-teddy/11.jpg
[image] => http://192.168.15.9/Kids_stores/image/cache/catalog/option-color/cream-images-50x50.jpg
[price] =>
[price_prefix] => +
)
)
[option_id] => 5
[name] => COLOR
[type] => image
[value] =>
[required] => 0
)
)
Try this,
foreach($array as $val)
{
echo $val['product_image'];
}
Solution for your edited input:-
$image_array = array();
foreach ($your_array as $arr){
$image_array[] = array_column($arr['product_option_value'],'product_image');
}
Output:- https://eval.in/657966
You can take the array array_column and make it like this
$records = array (
array (
// your array
)
);
$variable = array_column($records, 'image');
echo $variable;
<?php $samples=$data['options'][0][product_option_value];
$product_image = array_column($samples, 'product_image');
echo'<pre>'; print_r($product_image );
?>
foreach($array as $key => $val){
if($key == 'product_image'){
echo "<img src='".$val."' />";
}
}
Try
You have to foreach the inner array:
foreach($array[product_option_value] as $val)
{
echo $val['product_image'];
}
Solution One:
<?php
$req_image=array();
$req_image[] = array_column($resultant_array, 'product_image');
print_r($req_image); // This will print all the images that are grouped under the array().
?>
Example:
The below is the PHP code and the sample output that you can obtain using the array_column().
PHP:
<?php
$records = array(
array(
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe'
),
array(
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith'
),
array(
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones'
),
array(
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe'
)
);
$lastNames = array_column($records, 'last_name', 'id');
Output:
If we call print_r() on $lastNames, you’ll see a resulting array that looks a bit like this:
Array
(
[2135] => Doe
[3245] => Smith
[5342] => Jones
[5623] => Doe
)
Solution Two: (As per requirement at last)
You can iterate the single key value alone in the foreach so that you can get the required parameter that you need.
foreach($resultant_array as $single_array)
{
foreach($single_array['product_option_value'] as $inner_array)
{
echo $inner_array['product_image']; // This will print the u=mage name that you need.
}
}
If you want one specified key and want minimal assumptions about array structure shoud use array_walk_recursive like this
$result = [];
array_walk_recursive($input,function ($value,$key) use (&$result) {
if ( 'product_image' == $key) {
$result[] = $value;
}
});
Related
I need to convert the below 2d array in to specified 2d array format. Array contains multiple parent and multiple child array. Also, have tried to convert the code, but am not getting the expected output.
This is the code what i have tried,
$a1 = array(
'0' =>
array(
'banner_details' =>
array(
'id' => 2,
'section_id' => 24
),
'slide_details' =>
array(
0 => array(
'id' => 74,
'name' => 'Ads1'
),
1 => array(
'id' => 2,
'name' => 'Ads2'
)
)
),
'1' =>
array(
'banner_details' =>
array(
'id' => 106,
'section_id' => 92
),
'slide_details' =>
array(
0 => array(
'id' => 2001,
'name' => 'Adv1'
),
1 => array(
'id' => 2002,
'name' => 'Adv2'
)
)
)
);
$s = [];
for($i = 0; $i<2; $i++) {
foreach($a1[$i]['slide_details'] as $vs){
$s[] = $vs;
}
}
My output:
Array
(
[0] => Array
(
[id] => 74
[name] => Ads1
)
[1] => Array
(
[id] => 2
[name] => Ads2
)
[2] => Array
(
[id] => 2001
[name] => Adv1
)
[3] => Array
(
[id] => 2002
[name] => Adv2
)
)
Expected output:
Array
(
[24] => Array
(
[0] => 74
[1] => 2
)
[92] => Array
(
[0] => 2001
[1] => 2002
)
)
please check the above code and let me know.
Thanks,
You can apply next simple foreach loop with help of isset() function:
foreach($a1 as $data){
if (isset($data['banner_details']['section_id'])){
$s[$data['banner_details']['section_id']] = [];
if (isset($data['slide_details'])){
foreach($data['slide_details'] as $row){
$s[$data['banner_details']['section_id']][] = $row['id'];
}
}
}
}
Demo
If you know that indexes like banner_details or slide_details or section_id will be there always then you can skip isset() in if statements.
You can use array_column function for simple solution:
$result = [];
foreach ($a1 as $item)
{
$result[$item['banner_details']['section_id']] = array_column($item['slide_details'], 'id');
}
var_dump($result);
I have an array that contains two values, but i need upper value as key of lower value like value of name replace with value and also remove name from array.
Array
(
[0] => Array
(
[name] => firt_name
[value] => Robin
)
[1] => Array
(
[name] => last_name
[value] => Singh
)
[2] => Array
(
[name] => email
[value] => 123#gmail.com
)
[3] => Array
(
[name] => password
[value] => 12345
)
)
Here is the code
function key_replace($params = array())
{
if (!empty($params)) {
$array[] = array();
foreach ($params as $key => $value) {
$array[$value['name']] = $value['value'];
}
print_r($array);
}
}
Any solution appreciated!
Another approach is to use array_column and array_combine
array_combine(array_column($array, 'name'), array_column($array, 'value'));
https://3v4l.org/boAOI
A Simple foreach() will do the trick for you.
$result = [];
foreach($array as $k=>$v){
$result[$v['name']] = $v['value'];
}
print_r($result);
WORKING DEMO: https://3v4l.org/hH39i
$datas = $array = array
(
'0' => array
(
'name' => 'firt_name',
'value' => 'Robin'
)
,
'1' => array
(
'name' => 'last_name',
'value' => 'Singh'
)
,
'2' => array
(
'name' => 'email',
'value' => '123#gmail.com'
)
,
'3' => array
(
'name' => 'password',
'value' => '12345',
)
,
'4' => array
(
'name' => 'phone',
'value' => 'skdsjdkdjskd'
)
,
'5' => Array
(
'name' => 'city',
'value' => 'dskjdksjd'
)
,
'6' => Array
(
'name' => 'state',
'value' => 'kjksdjskdsk'
)
);
$array = '';
foreach ($datas as $key => $value) {
$array[$value['name']] = $value['value'];
}
echo '<pre>';
print_r($array);
echo '</pre>';
Array
(
[firt_name] => Robin
[last_name] => Singh
[email] => 123#gmail.com
[password] => 12345
[phone] => skdsjdkdjskd
[city] => dskjdksjd
[state] => kjksdjskdsk
)
convert multidimensional array to single dimension.
I have a multidimensional array like this..PHP using array finctions
Array
(
[0] => Array
(
[0] => Name1
[1] => valueOfName1
)
[0] => Array
(
[0] => Name2
[1] => valueOfName2
)
[2] => Array
(
[0] => Name3
[1] => valueOfName3
)
[3] => Array
(
[0] =>
)
[4] => Array
(
[0] => Name4
[1] => valueOfName4
)
[5] => Array
(
[0] =>
)
);
i want output like this..unsing any of function fo array
Array
(
Name1 => valueOfName1
Name2 => valueOfName2
Name3 => valueOfName3
Name4 => valueOfName4
)
Try this code:
$newArr = array();
foreach($mainArr as $key=>$value) {
if(isset($value[0]) && $value[0]!= '' && isset($value[1]) && $value[1] != '') {
$newArr[$value[0]] = $value[1];
}
}
print_r($newArr);
its simple... first try something befor you post a question...
$people = array (
"1" => array (
"0" => "greenspan",
"1" => 32
),
"2" => array (
"0" => "doe",
"1" => 52
)
);
$new_people = array();
while(list($person, $person_array) = each($people))
{
while(list($person_attribute, $value) = each($person_array))
{
$new_people[$person_attribute] = $value;
}
}
print_r($new_people);
Try out This Example:
<?php // Array representing a possible record set returned from a database
$records = array(
array(
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe',
),
array(
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith',
),
array(
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones',
),
array(
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe',
) );
$last_names = array_column($records, 'last_name', 'id');
print_r($last_names); ?>
Output :
Array (
[2135] => Doe
[3245] => Smith
[5342] => Jones
[5623] => Doe )
Hope this might be useful to you !!!
I have the following array which I would like to edit:
Array
(
[qty_black_34] =>
[qty_black_36] => 2
[qty_black_38] =>
[qty_black_40] =>
[qty_black_42] =>
[qty_black_44] =>
[qty_black_48] =>
[qty_powder_34] =>
[qty_powder_36] =>
[qty_powder_38] =>
[qty_powder_40] =>
[qty_powder_42] => 1
[qty_powder_44] =>
[qty_powder_48] =>
[qty_red_34] =>
[qty_red_36] =>
[qty_red_38] => 2
[qty_red_40] =>
[qty_red_42] =>
[qty_red_44] =>
[qty_red_48] => )
What I want to do is to build another array to hold only the elements with a value.
The new array must look like this"
Array
(
[0] => Array
(
[color] => black
[size] => 36
[quantity] => 2
)
[1] => Array
(
[color] => powder
[size] => 42
[quantity] => 1
)
[2] => Array
(
[color] => red
[size] => 38
[quantity] => 2
)
)
PHP is the language I'm using.
loop through array and take elements, which have set a value. for the additional key/values in your final array split the string used as key in original array
$new_array = array();
foreach ($old_array as $key => value) {
if ($value) {
$key_split = explode('_', $key);
$new_array[] = array('color' => $key_split[1], 'size' => $key_split[2], 'quantity' => $value);
}
}
$products = array();
foreach($quantities as $key => $quantity){
if($quantity != '') {
list($q, $color, $size) = explode('_', $key);
$products[] = array(
'color' => $color,
'size' => $size,
'quantity' => $quantity
);
}
}
Demo
I have an array $result as follows
Array
( [0] => Array (
[0] => Mr
[1] => vinay
[2] => hs
[3] => tester
[4] =>vinay.hs#abc.com
[5] => 909099
[6] => Yes )
[1] => Array (
[0] => Mr
[1] => Suresh
[2] => Kumar
[3] => tester
[4] => vinay.hs#abc.com
[5] => 809090
[6] => No )
).
I want to store this array as
Array
([0]=>Array (
[title] => Mr
[firstname] => vinay
[lastname] => hs
[job_title] => tester
[email] =>vinay.hs#abc.com
[phone] => 909099
[is_employed] => Yes )
[1] => Array (
[title] => Mr
[firstname] => Suresh
[lastname] => Kumar
[job_title] => tester
[email] => vinay.hs#abc.com
[phone] => 809090
[is_employed] => No ) ).
Explain me how to do this
$fp = fopen('foo.csv', 'r');
$fields = fgetcsv($fp); // assumes fields are the first row in the file
// or $fields = array('title', 'firstname', 'lastname', 'job_title', 'email', 'phone', 'is_employed');
$records = array();
while ($record = fgetcsv($fp))
{
$records[] = array_combine($fields, $record);
}
Obviously it needs error handling added to it.
$newarray=array();
foreach($result as $res) {
$a = array();
$i = 0;
foreach(array('title','firstname','lastname','job_title','email','phone','is_employed') as $key) {
$a[$key] = $res[$i++];
}
$newarray[] = $a;
}
$arr = array("title" => "Mr", "title" => "vinay", "key" => "value")
access it with:
$arr["title"]
array(
array(
'title' => 'Mr',
'firstname' => 'vinay',
'lastname' => 'hs',
'job_title' => 'tester',
'email' => 'vinay.hs#abc.com',
'phone' => '909099',
'is_employed' => TRUE
),
array(
'title' => 'Mr',
'firstname' => 'Suresh',
'lastname' => 'Kumar',
'job_title' => 'tester',
'email' => 'vinay.hs#abc.com',
'phone' => '809090',
'is_employed' => FALSE
)
);
UPDATED:
My answer previously is dumb. If you are loading from CSV file, assuming the first element of array is the keys.
You might want to do something like this.
Sorry for my bad naming.
$keysArray = array_shift($arrayFromCSV);
$wantedArrayStructure = array();
foreach ($arrayFromCSV as $person) {
$item = array();
foreach ($person as $key => $value) {
$item[$keysArray[$key]] = $value;
}
$wantedArrayStructure[] = $item;
}
var_dump($wantedArrayStructure);
If, is is result from msql query. You should using function to get the expected result:
mysql_fetch_assoc