Array inside an array(2 dimensional array) convert to string - php

How does one correctly/properly convert an array like this?
For example, if I do, print_r($array);
It would print out a result like,
Array([0] => Array([0] => 5))
How did that array come to be?
I know how to convert a single array to string by using implode(). It however, doesn't work on an array inside an array.
I don't think using implode() twice will do the trick. Does anyone have any idea?

if you want to get the array as string, use print_r with the second parameter true
$string = print_r($array, true);
or serialize
$string = serialize($array);
or json_encode
$string = json_encode($array);
And if you want to use implode, use this with array_walk_recursive
function test_print($item, $key)
{
if (is_array($item))
{
echo implode(',', $item);
}
}
array_walk_recursive($array, 'test_print');

Why not just loop it using a foreach construct ?
Something like this..
<?php
$arr = array(0=>array(0=>5),1=>array(0=>6));
foreach($arr as $arr1)
{
$str.=implode(' ',$arr1).",";
}
echo rtrim($str,','); //"prints" 5,6

Related

How to put PHP variable in array

$booked_seat=array(1,2);
if(in_array($seat,$booked_seat)){
$booked="red"; $book_seat="data-book='1'";
} else {
$booked=""; $book_seat="";
}
I want to put PHP variable in array
$booked_seat=array($id);
Is this possible in any way define variable in array?
You can do like this using explode() & array_shift() in php :
$str = '1,2,3';
$arr = [explode(",", $str)];
echo "<pre>";
print_r(array_shift($arr));
If you want a string instead of an array of numbers, you can use the join method http://php.net/manual/en/function.join.php.
$str = join(',', array(1, 2, 3));
If you are wanting to create an array out of a string like '1,2,3' you could use the explode method http://php.net/manual/en/function.explode.php.
$arr = explode(",", "1,2,3");

How to show values ONLY in php array?

I have a this:
$output = print_r($feedDrive, true);
But I just want the actual values, not the commas, or [0] => etc.
How can I do this?
There is foreach loop in php. You have to traverse the array.
foreach($array as $key => $value)
{
echo $value;
}
If you simply want to add commas between values, consider using implode
$string=implode(",",$array);
echo $string;

Get array number from Array with String value

I have an array with string value in PHP for example : arr['apple'], arr['banana'], and many more -about 20-30 data (get it from some process). Now I want to get its value and return it to one variable.
For example, I have Original array is like this:
$arr['Apple']
$arr['Banana']
and more..
and result that I want is like this:
$arr[0] = "Apple"
$arr[1] = "Banana"
and more..
Any idea how to do that?
Why not using array_keys()?
$new_array = array_keys($array);
Use array_flip()
$new_arr = array_flip($old_arr);
Demonstration
use foreach loop
foreach($arr as $key => $val){
$new_var[] = $key;
}
use array_keys function:
$keys = array_keys($arr);
It returns an array of all the keys in array.

List of objects

what is the best method to find the parent items of a comma separated string?
e.g.
array(1,2,3),array("test","123, abc"),1,"abc, 123"
to get
array(1,2,3)
array("test","123, abc")
1
"abc, 123"
Is this possible to get with a regular expression, or is there a nifty php function that will do this?
Use explode.
$myarray = explode(",",$original);
This is assuming that your original is a string, and your desired output is something you can easily iterate through:
$original = 'array(1,2,3),array("test","123, abc"),1,"abc, 123"';
$myarray = explode(",",$original);
foreach ($myarray as $item) {
echo $item."\n";
}
How about:
$arr = array(array(1,2,3),array("test","123, abc"),1,"abc, 123");
$newarray = array_chunk($arr, count($arr));
print_r($newarray);

array_map and htmlentities

I've been trying using array_map to convert characters to HTML entities with htmlentities() like this:
$lang = array_map('htmlentities', $lang);
My array looks like this:
$lang = array();
$lang['var_char1']['varchar2'] = 'Some Text';
But I keep getting this errors:
Warning: htmlentities() expects parameter 1 to be string, array given
in /home/user/public_html/foo/lang/en.inc.php on line 1335
Does anyone know what could be the problem? Thank you!
Use array_walk_recursive. array_map doesn't work with multidimensional arrays:
array_walk_recursive($lang, function (&$value) {
$value = htmlentities($value);
});
Because $lang is a two dimensional array, so it won't work
For two dimensional array you need to use for loop
foreach($$lang as &$l):
$l = array_map('htmlentities', $l);
}
$lang['var_char1']['varchar2'] defines a multidimensional array, so each element of $lang is also an array. array_map() iterates through $lang, passing an array to htmlentities() instead of a string.
array_map() doesn't work recursively. If you know your array is always two levels deep you could loop through it and use array_map on the sub-arrays.
if you like quotes
function stripslashes_array(&$arr) {
array_walk_recursive($arr, function (&$val) {
$val = htmlentities($val, ENT_QUOTES);
});
}
multiple array in post, get, dll
stripslashes_array($_POST);
stripslashes_array($_GET);
stripslashes_array($_REQUEST);
stripslashes_array($_COOKIE);
Each element in $lang is an array, so the function you pass to array_map should take an array as an argument. That is not the case for 'htmlentities' which takes a string.
You can:
$map_htmlentities = function(array) { return array_map('htmlentities', array); };
and then
$lang = array_map($map_htmlentities, $lang);
From PHP 7.4 on you can use lambdas:
$lang = array_map(fn($arr) => array_map('htmlentities', $arr), $lang);

Categories