I need to extract last array data i.e SiteName, Url, Title to the variable in php.
array
'ApplicableProductOfferings' =>
array
0 => string 'EasyDemo' (length=10)
'Artist' => string 'Hello' (length=10)
'ReferralDestinations' =>
array
0 =>
array
'SiteName' => string 'gettyimages' (length=11)
'Url' => string 'http://www.gettyimages.com/detail/160414706' (length=43)
'Title' => string 'Pixie Lott Launches The New BlackBerry Z10' (length=42)
'UrlComp' => string 'http://localhost.com' (length=197)
'UrlPreview' => string 'http://localhost.com' (length=164)
'UrlThumb' => string 'http://localhost.com' (length=82)
'UrlWatermarkComp' => string 'http://localhost.com' (length=197)
'UrlWatermarkPreview' => string 'http://localhost.com
try this.......
foreach($data as $dat)
{
foreach($dat['ReferralDestinations'] as $key => $d)
{
echo $d['SiteName'];
echo $d['Url'];
echo $d['Title'];
}
}
It depends on how you are storing the data in the array. If you have an array that already has the last array data, it's very simple.
foreach ($myArray as $var) {
echo $var;
}
or access individual elements as $myArray['SiteName'], $myArray['Url'] etc..
Assuming you have the above data in an array of arrays called $arrayOfArrays
foreach ($arrayOfArrays as $myArray) {
// $myArray now holds first array, second array etc as the loop is executed
// First time it holds 'ApplicableProductOfferings', second time 'ReferralDesinations'..
// If the array is 'ReferralDesitnations' you can loop through that array
// to get the elements you are looking for SiteName etc as below
foreach ($myArray as $URLElement) {
echo $URLElement;
}
}
$array1 = [];
$array2 = [];
foreach ($array1 as $key=> $val) {
extract($array2, EXTR_IF_EXISTS, $val);
}
// extract() flags => EXTR_IF_EXISTS ...
// http://php.net/manual/ru/function.extract.php
Related
I want to push array into existing session array. I would like to use get[id] and I want to be able stack all the arrays added rather than delete them when a new array is pushed.
Bellow is my code and I am not getting the value, instead I get this error --- Array to string conversion. Thanks in advance.
**CODE
<?php
session_start();
if(empty($_SESSION['animals']))
{
$_SESSION['animals']=array();
}
// push array
array_push($_SESSION['animals'], array ( 'id' => "".$_GET['id'].""));
foreach($_SESSION['animals'] as $key=>$value)
{
// and print out the values
echo $key;
echo $value;
}
?>
With your code, this is what $_SESSION looks like:
array (size=1)
'animals' =>
array (size=1)
0 =>
array (size=1)
'id' => string 'test' (length=4)
In your code :
foreach($_SESSION['animals'] as $key=>$value)
key will contain 0 and value will contain array('id' => 'test'). Since value is an array, you cannot echo it like this.
If you want to echo all the characteristics of each animal, this code will work :
<?php
session_start();
if(empty($_SESSION['animals']))
{
$_SESSION['animals'] = array();
}
// push array
array_push($_SESSION['animals'], array ( 'id' => "".$_GET['id'].""));
// We go through each animal
foreach($_SESSION['animals'] as $key=>$animal)
{
echo 'Animal n°'.$key;
// Inside each animal, go through each attibute
foreach ($animal as $attribute => $value)
{
echo $attribute;
echo $value;
}
}
Possibly already been asked but I can't seem to find the answer I'm looking for.
How can I check that a variable is in an array?
Here is my code so far - ideally I wan't to echo out something when the ProductID also exists in the lastUpdatedProduct array as well as the productOptions array.
<?php if ($this->productOptions) { ?>
<?php foreach ($this->productOptions as $options) {
$array = $this->lastUpdatedProduct;
echo '<strong>' . $options['ProductID'] . '</strong>';
if(in_array($options['CentreID'], $array)) {
echo 'it exists';
}
}
}?>
Heres my array:
array
0 =>
array
'pid' => string '391' (length=3)
1 =>
array
'pid' => string '467' (length=3)
2 =>
array
'pid' => string '474' (length=3)
3 =>
array
'pid' => string '2985' (length=4)
4 =>
array
'pid' => string '2985' (length=4)
5 =>
array
'pid' => string '424' (length=3)
The problem is that $array is an array with arrays and you are trying to find something inside one of the inner arrays. Try with this:
foreach ($array as $entry) {
if (is_array($entry) && in_array($options['CentreID'], $entry))
echo 'it exists';
}
Ideally I wan't to echo out something when the ProductID is in the array
if(is_array($options['ProductID'])) { }
Live Preview
Edit
Ideally I wan't to echo out something when the ProductID also exists in the lastUpdatedProduct array as well as the productOptions array.
if(is_array($this->productOptions)) {
foreach($array as $pid) {
if(in_array($pid, $options['ProductID'])) {
//echo
}
}
}
I have one array as below :
Array
(
[Sep] => Array
(
[Support Help Desk] => 24.67
[Development] => 7.74
[Consulting Services] => 4.04
)
[Oct] => Array
(
[Support Help Desk] => 14.38
[Business Activity] => 1.92
[Maintenance Tasks] => 1.00
[Development] => 2.11
)
)
and i want array like this :
Array
(
[Support Help Desk] => 24.67,14.38
[Development] => 7.74,2.11
[Consulting Services] => 4.04,0
[Business Activity] => 0,1.92
[Maintenance Tasks] => 0,1.00
)
I am using php with zend framework.
But i don't know what method should i use to get array like this ?
can anyone please guide me ?
-
Thanks in advance.
Third time lucky! I missed out on some subtleties in the question originally. Try the following code - it's a bit loopy but it should work for you.
I am assuming that your original array is called $data.
// first we need to 'normalise' or fill in the blanks in the contents of the sub array
// get a unique list of all the keys shared - doing it manually here
$keys = ['Support Help Desk', 'Business Activity', 'Maintenance Tasks', 'Development', 'Consulting Services'];
// create a default array with $keys, assigning 0 as the value of each
$default = array_fill_keys($keys, 0);
// next fill in the blanks...
// get the diff (missing keys) between the current element and the default array
// merge the missing key/value pairs
array_walk($data, function(&$month, $key, $default) {
$diff = array_diff_key($default, $month);
$month = array_merge($diff, $month);
}, $default);
// now the array is normalised
// flatten the array... where there are duplicate values for a key, and
// there will be in all cases now including default values
// a sub array is created
$merged = call_user_func_array('array_merge_recursive', $data);
// finally loop over the merged array
// and implode each array of values into a comma separated list
foreach ($merged as &$element) {
if (is_array($element)) {
$element = implode(', ', $element);
}
}
// done :)
var_dump($merged);
Yields:
array (size=5)
'Business Activity' => string '0, 1.92' (length=7)
'Maintenance Tasks' => string '0, 1' (length=4)
'Support Help Desk' => string '24.67, 14.38' (length=12)
'Development' => string '7.74, 2.11' (length=10)
'Consulting Services' => &string '4.04, 0' (length=7)
Hope this helps :)
EDIT
Live example at eval.in
Let's say your array is stored in $main_arr and result array is $result_arr.
$result_arr = array();
foreach ($main_arr as $month) {
foreach ($month as $key => $val) {
if (!isset($result_arr[$key])) {
$result_arr[$key] = array($val);
} else {
array_push($result_arr[$key], $val);
}
}
}
foreach ($result_arr as $key => $val) {
$result_arr[$key] = implode(', ', $val);
}
print_r($result_arr); //Final output.
I have several values stored in $_SESSION beginning with 'first_name_' & 'last_name_' which then a number appended to the end deepening on how many names are generated.
I am able to extract each of these values from the session and add to an array but would like to pair up the first and last names together within a nested array. (if that makes sense)
at the moment I have:
$users_array = array();
foreach ($_SESSION as $key => $value) {
if(strpos($key, 'first_name_') === 0) {
$users_array[] = $value;
}
if(strpos($key, 'last_name_') === 0) {
$users_array[] = $value;
}
}
This produces an output with var_dump:
array
0 => string 'John' (length=4)
1 => string 'Smith' (length=8)
2 => string 'Jane' (length=4)
3 => string 'Doe' (length=3)
But what I would like is something like:
array
'user' =>
array
'first_name' => string 'John' (length=4)
'last_name' => string 'Smith' (length=5)
array
'first_name' => string 'Jane' (length=4)
'last_name' => string 'Doe' (length=5)
Any suggestions on how I can achieve this?
deceze is right in his comment... But just so people with similar issues with creating 2-dimensional array from a 1-dimensional array, here is a solution. Also, note that PHP does not guarantee that the order will be same when iterating using FOREACH. As this will work, it is still prone to errors.
$users_array = array();
foreach ($_SESSION as $key => $value) {
if(strpos($key, 'first_name_') === 0) {
$users_array[] = array();
$users_array[count($users_array)-1]['first_name'] = $value;
}
if(strpos($key, 'last_name_') === 0) {
$users_array[count($users_array)-1]['last_name'] = $value;
}
}
I'm trying to create an array, looping through another array and taking the string value within the key to assigning it as a key/value pair in the new array. Here's a sample of the array of values I'm using outputted via var_dump:
array
0 => string 'dog,bark' (length=8)
1 => string 'cat,meow' (length=8)
2 => string 'cow,moo' (length=7)
What I want to do have it so in the new array, it is set up as such
array
'dog' => string 'bark' (length=4)
'cat' => string 'meow' (length=4)
'cow' => string 'moo' (length=3)
I thought that explode would do the trick, delimiting by commas, but it doesn't populate the keys as intended, instead using the standard numerical values. So after doing some research and coming up blank, i'm wonder if there's a php function that i'm missing, or have missed some simple amount of logic that would do what i'm after.
EDIT: Forgot the most important part. Here's the current code that is assigning values to the new array. One reason not to code at 2am
foreach ($array as $key=>$value) {
$newArray= explode(',', $array [$key]);
}
$start = array('a,x', 'b,y', 'c,z');
$result = array();
foreach($start as $startVal){
list($key,$val) = explode(',', $startVal);
$result[$key] = $val;
}
$newArray = array();
foreach($array as $joined) {
list($key, $value) = explode(',', $joined);
$newArray[$key] = $value;
}