PHP: How to echo strings from an Array? - arrays

I'm stuck trying to echo strings from an array, all I get is "Array" as text.
This is the array:
$_SESSION['lista'][] = array(
'articulo' => $articulo,
'precio' => $precio,
'cantidad' => $cantidad);
This is the echo:
echo "1. ".$_SESSION['lista'][0][0]." ".$_SESSION['lista'][0][1]." unidades".", ".$_SESSION['lista'][0][2]." CRC.";
The current output is:
1. Array Array unidades, Array CRC.

Remove [] so it looks like these
And put session_start() at starting line;
<?php
session_start();
$_SESSION['lista'] = array(
'articulo' => $articulo,
'precio' => $precio,
'cantidad' => $cantidad);
?>
To access the array:
echo $_SESSION['lista']['articulo'];
echo $_SESSION['lista']['precio'];

You can't access an associative array member with a numerical key as an offset.
Try this...
echo $_SESSION['lista'][0]['articulo'];
An array's to string type method is called (and returns Array) when you try to implicitly convert it to a string, e.g. with echo.

Take a look at print_r along with var_dump etc. As stated in the manual, these functions print the contents of arrays/objects in human readable format.

Related

How do i create array and put value on it - BASH script

ahhh array and loops my weakest links. I was trying to create array depending on user input so
printf "%s\n" "how may array you want"
read value
after this i will ask what value user want to put on a array(this is the bit im stuck on)
i=1
while [ $i -le $value ]
do
echo "what value you want to put in array $i"
read number
echo $number >> array.db
i=$(( i+1 ))
echo
done
although this method works(i think) but i'm not too sure if i'm actually creating an array and putting value to that array.
you can expand arrays in bash dynamically. you can use this snippet
a=(); a[${#a[#]}]=${number}; echo ${a[#]}
The first statement defines an empty array. with the second (which you can use in your while loop) you insert a value at last elment position + 1, due to ${#a[#]} represents the length of a. the third statement just prints all elements in the array.

combobox input of form using array variable in CakePHP

<?php
$types_size=sizeof($types)-1;
$array_type=' ';
while($types_size!=-1)
{
$array_type.='"'.$types[$types_size]["Type"]["name_type"].'", ';
$types_size--;
}
echo $array_type;
echo $this -> form -> input ('id_type', array('options' => array($array_type)));
echo $this -> form -> input ('id_type', array('options' => array(shoes, shirts, jeans,)));
?>
I created $array_type to store types data.
I got the types data : "shoes", "shirts", "jeans",
I wanna created an combobox input with 3 values just like the second input
Question : The code above doesn't show as I expected. The combobox of the first input shows only one value: shoes, shirts, jeans, not like the expected second input
Hopefully someone understand my problem !!!
In the above code you will be realized that $array_type is a string variable not an array.
You should declare an array variable using $array_type = array() not $array_type = ''
You can try to print $array_type variable and verify whether it is an array or a string.
Now your code should looks like:
<?php
$types_size=sizeof($types)-1;
$array_type= array();
while($types_size!=-1)
{
$array_type[] = $types[$types_size]["Type"]["name_type"];
$types_size--;
}
//echo $array_type; // if you echo an array variable it will print `Array`.
//Use print_r() to print an array
print_r($array_type);
echo $this -> form -> input ('id_type', array('options' => $array_type));
echo $this -> form -> input ('id_type', array('options' => array(shoes, shirts, jeans,)));
?>
<?php
$types_size=sizeof($types)-1;
$array_type= array();
while($types_size!=-1)
{
$array_type[] = $types[$types_size]["Type"]["name_type"];
$types_size--;
}
//echo $array_type; // if you echo an array variable it will print `Array`.
//Use print_r() to print an array
print_r($array_type);
echo $this -> form -> input ('id_type', array('options' => $array_type));
echo $this -> form -> input ('id_type', array('options' => array(shoes, shirts, jeans,)));

convert a php array with key in a string?

I am novice in php development and I need your help.
I have read the column of a file and I want to keep in an array
only the distinct values
I do it with this code in a while (fgetcsv) loop
$sex_array = array();
isset($sex_array[$sex])?$sex_array[$sex]++:$sex_array[$sex]=1;
my array is in format
Array ( [man] => 33 [woman] => 141 )
How can I make a variable and when I print it to take
man:33,woman:141
I tried implode but I take 33,141
look at foreach taken from the page:
foreach ($a as $k => $v) {
echo "\$a[$k] => $v.\n";
}

CakePHP echo first and last record of array output foreach() iteration

I am able to access and output a full array list of Zip items like so (this is working as expected):
... (this is a foreach within a foreach)
foreach ($plan_edit['Zip'] as $zip) :
echo $zip['title'] . "<br />";
endforeach; ...
Returns:
Array
(
[0] => Array
(
[id] => 110
[state_id] => 1
[title] => 97701
[PlansZip] => Array
(
[id] => 83698
[plan_id] => 443
[zip_id] => 110
)
)
[1]
I am trying to ONLY get the first and last value (of ['title']) of each array set for each main record.
I've been messing around with phps array current() and end() functions, but I can only get "Array
" to print out with those.
I know I am doing something wrong, but kind of lost direction at this point.
Any constructive criticism of my work/methods is welcome.
This is where I am at currently:
<?php
foreach ($plan_edit['Zip'] as $zip) :
echo current($zip['title']) . "<br />";
endforeach;
foreach ($plan_edit['Zip'] as $zip) :
echo end($zip['title']) . "<br />";
endforeach;
?>
$first = reset($plan_edit['Zip']);
$last = end($plan_edit['Zip']);
echo $first['title'];
echo $last['title'];
If the array is numerically indexed, you can also just do:
echo $plan_edit['Zip'][0]['title'];
echo $plan_edit['Zip'][count($plan_edit['Zip']) - 1]['title'];

preg_replace multiple problem

I am trying to replace multiple urls in a pagination element with a new url.
The replacement works fine when there is one occurrence of the url i.e. prev and next, but it breaks on breaks on the Pagination Numbers. It brings the first and last number links together. How do I get the preg_replace function realize that there are multiple occurrences of the links that need replacing?
<?php
$pattern = '/\/(.+)\/page:(\d)/S';
$replacement = $uurl.'page:$2';
echo preg_replace($pattern, $replacement,$paginator->prev('<< '.__('previous', true), array('url' => $paginator->params['pass']), null, array('class'=>'disabled'))).' | ';
echo preg_replace($pattern, $replacement,$paginator->numbers());
echo preg_replace($pattern, $replacement,$paginator->next(__('next', true).' >>', array('url' => $paginator->params['pass']), null, array('class'=>'disabled')));
?>
Try this:
$pattern = '/\/(.+?)\/page:(\d)/S';
Your .+ is greedy. In other words, it's sucking up everything between the first / and the last /page:.
The ? operator will make it match the minimum instead.

Resources