Below are the result of array
Array
(
[0] => Array
(
[0] => test
[1] => cate
[2] => category
)
)
I want the result to be
$availableTags = ["test","cate","category"];
How to do this in codeigniter php
It's nothing code-igniter specific. The array currently looks like this: [["test","cate","category"]], so just do $availableTags = $originalArray[0];
To copy:
$availableTags = array_slice($originalArray[0],0);
And to stringify:
echo json_encode($availableTags);
$array = $array[0];
Should be enough
$array = [["test","cate","category"]];
var_dump ($array);
$array = $array[0];
var_dump ($array);
Output :
array(1) { [0]=> array(3) { [0]=> string(4) "test" [1]=> string(4) "cate" [2]=> string(8) "category" } }
array(3) { [0]=> string(4) "test" [1]=> string(4) "cate" [2]=> string(8) "category" }
Short and simpler way [A Mix of json_encode and str_replace and explode]
<?php
$data = array('foo',
'baz',
'cow',
'php',
array('bar','dog','xml'));
//Results as string
$stringRes = str_replace(array('[', '"',']'), '' , json_encode($data));
//Results as array
$arrayRes = explode(',', str_replace(array('[', '"',']'), '' , json_encode($data)));
?>
OUTPUT
foo,baz,cow,php,bar,dog,xml
Array ( [0] => foo [1] => baz [2] => cow [3] => php [4] => bar [5] => dog [6] => xml )
Related
I add arrays with sub arrays value data into mysql database like this:
{"2":[
{"title":"english title","link":"english url"},
{"title":"","link":""}],
"1":[
{"title":"french title","link":"french url"},
{"title":"","link":""}]}
I try to remove empty sub array value using array_filter and array_map like this:
var_dump(array_filter(array_map('array_filter', $links)));
now, when i check using var_dump I see this result and array_filter and array_map can not remove empty value:
array(2) {
[2]=>
array(3) {
[0]=>
array(2) {
["title"]=>
string(13) "english title"
["link"]=>
string(11) "english url"
}
[1]=>
array(2) {
["title"]=>
string(0) ""
["link"]=>
string(0) ""
}
}
[1]=>
array(3) {
[0]=>
array(2) {
["title"]=>
string(12) "french title"
["link"]=>
string(10) "french url"
}
[1]=>
array(2) {
["title"]=>
string(0) ""
["link"]=>
string(0) ""
}
}
}
in action, how do can i remove empty sub arrays value ?!
UPDATE: i need to this result:
Array
(
[2] => Array
(
[0] => Array
(
[title] => english title
[link] => english url
)
)
[1] => Array
(
[0] => Array
(
[title] => french title
[link] => french url
)
)
)
Your problem is that the items you are trying to filter are not empty (empty($links[2][1]) === false), they are a two element array, so array_filter will not remove them. Instead you need to check that both elements are blank. For example, using array_walk:
array_walk($links, function (&$array) {
$array = array_filter($array, function ($obj) {
return $obj['title'] != '' || $obj['link'] != '';
});
});
print_r($links);
Output:
Array
(
[2] => Array
(
[0] => Array
(
[title] => english title
[link] => english url
)
)
[1] => Array
(
[0] => Array
(
[title] => french title
[link] => french url
)
)
)
Demo on 3v4l.org
I have 2 PHP arrays that I need to combine values together.
First Array
array(2) {
[0]=>
array(1) {
["id"]=>
string(1) "1"
}
[1]=>
array(1) {
["id"]=>
string(2) "40"
}
}
Second Array
array(2) {
[0]=>
string(4) "1008"
[1]=>
string(1) "4"
}
Output desired
array(2) {
[0]=>
array(1) {
["id"]=>
string(1) "1",
["count"]=>
string(1) "1008"
}
[1]=>
array(1) {
["id"]=>
string(2) "40",
["count"]=>
string(1) "4"
}
}
As you can see I need to add a new key name (count) to my second array and combine values to my first array.
What can I do to output this array combined?
Try something like the following. The idea is to iterate on the first array and for each array index add a new key "count" that holds the value contained on the same index of the second array.
$array1 = [];
$array2 = [];
for ($i = 0; $i < count($array1); $i++) {
$array1[$i]['count'] = $array2[$i];
}
you can do it like this
$arr1=[["id"=>1],["id"=>40]];
$arr2=[1008,4];
for($i=0;$i<count($arr2);$i++){
$arr1[$i]["count"] = $arr2[$i];
}
Live demo : https://eval.in/904266
output is
Array
(
[0] => Array
(
[id] => 1
[count] => 1008
)
[1] => Array
(
[id] => 40
[count] => 4
)
)
Another functional approach (this won't mutate/change the initial arrays):
$arr1 = [['id'=> "1"], ['id'=> "40"]];
$arr2 = ["1008", "4"];
$result = array_map(function($a){
return array_combine(['id', 'count'], $a);
}, array_map(null, array_column($arr1, 'id'), $arr2));
print_r($result);
The output:
Array
(
[0] => Array
(
[id] => 1
[count] => 1008
)
[1] => Array
(
[id] => 40
[count] => 4
)
)
Or another approach with recursion:
$arr1=[["id"=>1],["id"=>40]];
$arr2=[1008,4];
foreach ($arr1 as $key=>$value) {
$result[] = array_merge_recursive($arr1[$key], array ("count" => $arr2[$key]));
}
print_r($result);
And output:
Array
(
[0] => Array
(
[id] => 1
[count] => 1008
)
[1] => Array
(
[id] => 40
[count] => 4
)
)
So far: I'm doing a preg_match_all search and my output is: Donald, Daisy, Huey, Dewey and Louie
Code is like this:
$duckburg = array();
preg_match_all($pattern,$subject,$match);
$duckburg['residents'] = $match[1];
print_r($duckburg['residents']);
Output:
Array ( [residents] => Array ( [0] => Donald [1] => Daisy [2] => Huey [3] => Dewey [4] => Louie )
My question: I would like to add to every string " Duck"
Using this help-string: $lastname = " Duck"
The output should be:
Array ( [residents] => Array ( [0] => Donald Duck [1] => Daisy Duck [2] => Huey Duck [3] => Dewey Duck [4] => Louie Duck )
I tried (but it's not working):
preg_match_all($pattern,$subject,$match);
$matchy = $match.$lastname;
$duckburg['residents'] = $matchy[1];
print_r($duckburg['residents']);
Is it possible to change the matching string before it goes into the array? Thank you for your help!
Array_map is the tool for such manipulation:
$match = Array ( 'residents' => Array ('Donald','Daisy','Huey','Dewey','Louie'));
$duckburg['residents'] = array_map(function($n) { return "$n Duck"; }, $match['residents']);
var_dump($duckburg);
Output:
array(1) {
["residents"]=>
array(5) {
[0]=>
string(11) "Donald Duck"
[1]=>
string(10) "Daisy Duck"
[2]=>
string(9) "Huey Duck"
[3]=>
string(10) "Dewey Duck"
[4]=>
string(10) "Louie Duck"
}
}
Iterate over the array is one possible option:
$lastname = " Duck";
preg_match_all($pattern,$subject,$matches);
foreach($matches as $key => $val){
$duckburg['residents'][$key] = $val . $lastname;
}
print_r($duckburg['residents']);
I am using the results from a db query to populate an array. If I do a var_dump on the array, it looks fine. However, if I try to access the elements of the array by echoing $myArray[0] or any other element in the array, all I get is array.
Here is an excerpt of my code.
$losers = array();
if ($result=mysqli_store_result($con))
{
while ($row=mysqli_fetch_row($result))
{
//printf("%s\n",$row['Winner']);
if($row[0]!= "MLB"){
$data[] = $row;
echo $row[0] . '<br />';
Where I am using the echo for the row element, no problems, it does fine. Here are the results of my var_dump
array(4) { [0] => array(4) { [0] => string(3) "MLB" [1] => string(15) "Cincinnati Reds" [2] => string(4) "-137" [3] => string(88) "images/mlb/cred.jpg" } [1] => array(4) { [0] => string(3) "MLB" [1] => string(15) "Minnesota Twins" [2] => string(4) "-128" [3] => string(88) "images/mlb/mtwi.jpg" } [2] => array(4) { [0] => string(3) "MLB" [1] => string(14) "Atlanta Braves" [2] => string(4) "-101" [3] => string(88) "images/mlb/Abra.jpg" } [3] => array(4) { [0] => string(3) "MLB" [1] => string(20) "Washington Nationals" [2] => string(4) "-140" [3] => string(88) "images/mlb/wnat.jpg" } }
And this is what happens when I use echo to show the results of the array.
ArrayArrayArrayArrayArrayArrayArrayArrayArrayArray
That is obviously because element 0 is actually an array. When you try to echo an array, not surprisingly, you get Array and a warning. To print an array use print_r($array, true);
$losers = array();
if ($result=mysqli_store_result($con))
{
while ($row=mysqli_fetch_row($result))
{
//printf("%s\n",$row['Winner']);
if($row[0]!= "MLB"){
$data[] = $row;
echo print_r($row[0], true) . '<br />';
I have this ARRAY sent from a FORM
Array
(
[0] => one
two
three
[1] => hello
how
are
)
Since the values are sent via textarea from a form, it has line breakers, so I want to make change every into a array field like so:
Array
(
[0] => Array
(
[0] => one
[1] => two
[2] => three
)
[1] => Array
(
[0] => hello
[1] => how
[2] => are
)
)
This is what I have so far:
foreach ($array as &$valor) {
foreach(preg_split("/((\r?\n)|(\r\n?))/", $valor) as $line){
$arraynew[] = $line;
}
}
But I get this result
Array
(
[0] => one
[1] => two
[2] => three
[3] => hello
[4] => how
[5] => are
)
What am I doing wrong? :(
For each key in the POST array, pop a value onto the final array, with it's values containing an array whoses values represent each line in value of the POST array.
<?php
$data = array(
0 => "one\ntwo\nthree",
1 => "hello\nhow\nare"
);
$final = array();
for($i = 0; $i < count($data); $i++) {
$row = preg_split('/\n/', $data[$i]);
if(is_array($row)) {
for($j = 0; $j < count($row); $j++) {
$final[$i][] = $row[$j];
}
}
}
var_dump($final);
array(2) {
[0] =>
array(3) {
[0] =>
string(3) "one"
[1] =>
string(3) "two"
[2] =>
string(5) "three"
}
[1] =>
array(3) {
[0] =>
string(5) "hello"
[1] =>
string(3) "how"
[2] =>
string(3) "are"
}
}
DEMO
Well, you're really working too hard.
array_map(function($e) { return explode("\n", $e); }, $orig_array);
is all you need. You could use preg_split if you really want, but explode is enough.
You can simply do this
$array=array("one\ntwo\nthree","hello\nhow\nare");
$arraynew=array();
foreach ($array as &$valor) {
$arraynewtemp=array();
foreach(preg_split("/((\r?\n)|(\r\n?))/", $valor) as $line){
$arraynewtemp[] = $line;
}
array_push($arraynew,$arraynewtemp);
}
var_dump($arraynew);
Will output
array(2) {
[0]=>
array(3) {
[0]=>
string(3) "one"
[1]=>
string(3) "two"
[2]=>
string(5) "three"
}
[1]=>
array(3) {
[0]=>
string(5) "hello"
[1]=>
string(3) "how"
[2]=>
string(3) "are"
}
}
This should works