Currently my JSON outputs from the following PHP:
$data['products'][] = array(
'product_id' => $result['product_id'],
'thumb' => $image,
'name' => $result['name'],
'description' => $desc,
'price' => $price,
'special' => $special,
'tax' => $tax,
);
And with this ($products = json_encode ($data['products']);) produces the following:
[{"product_id":"28",
"thumb":"x",
"name":"name",
"description":"abc",
"price":"$123.00",
"special":false,
"tax":"$100.00"}]
Is it possible to delete the names without modifying the php "$data['products'][] = array();"? I am trying to achieve:
["28",
"x",
"name",
"abc",
"$123.00",
false,
"$100.00"]
First time ever using JSON encode so any other advice would be awesome!
You can use array_map to loop thru your array and use array_values as callback function to convert the associative array to simple array
$arr = array_map('array_values', $data['products'] );
$products = json_encode ($arr);
This will result to:
[["28","x","name","abc","$123.00",false,"$100.00"]]
Live Example
You can use array_values to get the values of the first/only entry in $data['products'], and then encode that:
$json = json_encode(array_values($data['products'][0]));
That produces
["28","x","name","abc","$123.00",false,"$100.00"]
Live Example
Related
I am new in PHP. I am working for send data using PHP API. Its method is like below
$createContact->setCustomFieldValues(
[
array(
'customFieldId' => 'pcVFD6',
'value' => array('35')
),
array(
'customFieldId' => 'pcVnzW',
'value' => array('37')
)
]
);
I have data in array like this
$aa = array("pcVFD6"=>"35", "pcVnzW"=>"37");
I want pass this values to above function but I am not getting idea about proper way to do it.
I have tried something like this
foreach ($aa as $key => $value){
$createContact->setCustomFieldValues([
array(
'customFieldId' => $key,
'value' => array($value)
)
]
);
}
its working but passing only last one array to API. Anyone here can please help me for achieve my goal?
Thanks!
You need to transform the array before you pass it to setCustomFieldValues. You don't want to be calling setCustomFieldValues multiple times as your current attempt does, as that's not equivalent to the original. You just need to change the structure of the array ahead of time.
For example:
$aa = array("pcVFD6"=>"35", "pcVnzW"=>"37");
$transformedArr = array();
foreach ($aa as $key => $value){
$transformedArr[] = array(
'customFieldId' => $key,
'value' => array($value)
);
}
$createContact->setCustomFieldValues($transformedArr);
I have this multidimensional array in php:
$products = array(array(
"name" => "Hannah",
"id" => "eg01",
"price" => 120
),
array(
"name" => "Natasha",
"id" => "eg02",
"price" => 125
));
How do I push new data in the array and save it?
I tried this code:
array_push($products, $name, $id, $price);
but it only replaces the new one every time I click the push button.
I wanted it to be saved every time i push.
Either you can use array_push() method for inserting records in an array or we can append data like this
$products[] = ['key'=>'value'];
Your array is made of sub arrays with named keys, you need to construct your inner array first.
array_push($products,array('name'=>$name,'id'=>$id,'price'=>$price));
or for verbosity
$item = array(
'name' => $name,
'id' => $id,
'price' => $price
);
array_push( $products, $item );
you may also see this syntax
$products[] = array('name'=>$name,'id'=>$id,'price'=>$price);
in which you can optionally specify a key if needed between the [] notation
I'm writing a php web application where I have a nested array which looks similar to the following:
$results = array(
array(
array(
'ID' => 1,
'Name' => 'Hi'
)
),
array(
array(
'ID' => 2,
'Name' => 'Hello'
)
),
array(
array(
'ID' => 3,
'Name' => 'Hey'
)
)
);
Currently this means that when I want to use the ID field I have to call $results[0][0]['ID'] which is rather inefficient and with an array of over several hundred records becomes messy quickly. I would like to shrink the array down so that I can call $results[0]['ID'] instead.
My understanding is that a function that uses a foreach loop to iterate through each row in the array and change the format would be the best way to go about changing the format of the $results array but I am struggling to understand what to do after the foreach loop has each initial array.
Here is the code I have so far:
public function filterArray($results) {
$outputArray = array();
foreach ($results as $key => $row) {
}
return $outputArray;
}
Would anyone be able to suggest the most effective way to achieve what I am after?
Thanks :)
Simply use call_user_func_array as
$array = call_user_func_array('array_merge', $results);
print_r($array);
Demo
I would like to use a php array in this example rather than have to hard code the array. How would I use a php to replace this json example?
Here the working code with the json array:
$json_string = array(
'to' => array(
'example1#sendgrid.com', 'example2#sendgrid.com'
),
'category' => 'test_category'
);
But I need to replace the 'to' values with my own php array. I tried this but it doesn't work:
$myEmails[] = array('emailOne#gmail.com','email2#gmail.com');
$json_string = array(
'to' => $myEmails, /// DOES NOT WORK
'category' => 'test_category'
);
What code for the json could I use to add my own php array values here? In short I am trying to send multiple emails using sendgrid but I thought this might work but its not.
Just get rid of the square braces [] after $myEmails, and everything should work:
<?php
$myEmails = array('emailOne#gmail.com','email2#gmail.com');
$json_string = array(
'to' => $myEmails, /// DOES NOT WORK
'category' => 'test_category'
);
var_dump($json_string);
?>
got a multidimensional array and a string in a config and i need to transform it as an array key without the use of eval. Real world use of this problems is that i got a big document from mongodb that is transformed into multi dimensional array. However i need to define specific array nodes from a config file.
the idea is to create a config file as representation of the array key's hierarchy
on the config.ini the values below are some example.
colorattribute = attribute.color
wholesaleprice = prices.wholesale
Example Response from mongoDb
<?php
$products = array(
'product_name' => 'iTouch',
'brand_name' => 'Apple',
'attributes' => array ( 'color' => 'black',
'size' => '5 in'
),
'prices' => array(
'wholesale' => 135,
'retail' => 200,
),
);
function recurseKeys(array $keys,array $array){
$key = array_shift($keys);
if(!isset($array[$key])) return null;
return empty($keys) ?
$array[$key]:
recurseKeys($keys,$array[$key];
}
var_dump(recurseKeys(explode('.',$testConfig),$products);