I have an issue and trying to solve in php. I need ideas of yours for solving this issue.
I'm gonna try to explain, hope I can.
I have an array like this:
$mainArr = array(array("a","b","c"),array("1","2","3"),array("x","y","z"));
Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
)
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[2] => Array
(
[0] => x
[1] => y
[2] => z
)
)
What I'm trying to do is printing all 3 possibilities of leaf elements. Like that:
a1x
a1y
a1z
a2x
a2y
a2z
a3x
a3y
a3z
b1x
b1y
b1z
.
.
.
I try to form an algorithm to make this, but I got stuck.
Can you please help me for this issue?
Thanks in advance
You should check RecursiveIteratorIterator
I believe this is solution for your problem.
foreach ($mainArr[0] as $level1)
foreach ($mainArr[1] as $level2)
foreach ($mainArr[2] as $level3)
echo $level1 . $level2 . $level3 . "<br />";
Related
I'm consuming an API which returns an array of objects as this:
$base = array(
["orange","_","banana"],
["banana","_","_"],
["_","apple","kiwi"],
["_","raspberry","strawberry"]
);
And I intend to show "0" when key value is "_" however I haven't found a better way to do this than this:
foreach ($base as $key => $value) {
for ($i=0; $i<=3;$i++) {
if ($base[$key][$i]=="_")
$base[$key][$i]="0";
}
}
This works just fine since it's a simple demo but the real array is sometimes big and I've found this solution somewhat inefficient.
My question is, there's some php built-in function to do achieve this in or at least a better way to do this?
Thanks in advance guys,
Use array_walk_recursive(), pass the elements by reference and walk over the array, checking for the value _ - if its a match, replace it with 0.
$base = array(
["orange","_","banana"],
["banana","_","_"],
["_","apple","kiwi"],
["_","raspberry","strawberry"]
);
array_walk_recursive($base, function(&$v) {
if ($v === '_')
$v = 0;
});
Output becomes
Array
(
[0] => Array
(
[0] => orange
[1] => 0
[2] => banana
)
[1] => Array
(
[0] => banana
[1] => 0
[2] => 0
)
[2] => Array
(
[0] => 0
[1] => apple
[2] => kiwi
)
[3] => Array
(
[0] => 0
[1] => raspberry
[2] => strawberry
)
)
Live demo at https://3v4l.org/6Bs8ZE
You can replace _ with 0;
json_decode(str_replace('"_"','"0"',json_encode($base)));
I have a array like this
[0] => Array
(
[0] => LBLdss_COLLEsdfCTIONS_RsdfsdEPORT_TITfsdfLE
[1] =>
[2] =>
[3] => Array
(
[Administration] => Array
(
[bidsflldsf6] => Array
(
[0] => themes/Care2012/images/Payments
[1] => LsdfBL_OPsddfD_BIsfdfsLLING_SsdfdsUMsdfMARY_TITLE
[2] => LsdfsdBL_OPDfdsfd_BILfdsLING_dsfdsSUMMARY
[3] => ./index.php?module=Rsdfepfdsforts&action=reposfdfdsrtsel&rname=Opdpasfdfypdf
)
[bilhghjgl_pat_reg] => Array
(
[0] => themsdfsdes/Casfdfre2aasd012/imasdfges/Pasdymesddfnts
[1] => LBL_sdfsPAT_RsdfEG_TsdfITLE
[2] => LBL_PdfsdAT_sfdREG_TdsfdITLE_DsdfsETAIL
[3] => ./index.php?module=Rexcvpofdsrts&action=reportsel&rname=Pacxvtregcollxcvectionpdf
)
)
)
[4] =>
)
Now i need to extract value of rname from this index [3] => ./index.php?module=Rexcvpofdsrts&action=reportsel&rname=Pacxvtregcollxcvectionpdf (which is Pacxvtregcollxcvectionpdf in this case)and have to save it
I can try explode function but it is heavy for me since my array size is large
please help in resolving this
Thanks
Try placing a foreach for ciclying the array and formulate a regular expression for extract your param. Like this:
foreach($youvar as $text) {
preg_match('$rname=(.+?)$', $text, $rname[])
}
After that you can try a print_r on $rname and adapt to you.
Try below :-
$a = explode("rname=", index[3]);
echo $a[1;]
Code
$array = array(
'./index.php?module=Rsdfepfdsforts&action=reposfdfdsrtsel&rname=Opdpasfdfypdf',
'./index.php?module=Rexcvpofdsrts&action=reportsel&rname=Pacxvtregcollxcvectionpdf',
);
function getRname($str){
//return null if not found
preg_match('/rname\=([a-zA-Z]+)($|&)/', $str , $matches);
return $matches[1];
}
foreach($array as $str){
echo "Rname: " . getRname($str). "<br/>";
}
Result
Rname: Opdpasfdfypdf
Rname: Pacxvtregcollxcvectionpdf
Try it?
Can anyone help me with this little problem please? I need to get (echo) the name, id and link from an array but after hours trying I have not been able, see the array below... Thank you in advanced.
Array
(
[campaigns] => Array
(
[0] => Campaign Object
(
[name] => My name 1
[id] => 123456789012
[link] => 123456789012
)
[1] => Campaign Object
(
[name] => My name 2
[id] => 123456789012
[link] => 123456789012
)
[2] => Campaign Object
(
[name] => My name 3
[id] => 123456789012
[link] => 123456789012
)
)
)
If you know which campaign you want, you can get it like this:
echo($data['campaigns'][0]->name);
echo($data['campaigns'][0]->id);
echo($data['campaigns'][0]->link);
If you want to loop through all of them, you could do something like this:
foreach ($data['campaigns'] as $item) {
echo($item->name . "\n");
echo($item->id . "\n");
echo($item->link . "\n");
}
This is all a bit of a guess because we don't know what the Campaign class actually looks like - there may be a getName() method that you should be using instead of just accessing the name value directly, for example.
foreach ($array['campaigns'] as $key => $value){
echo "Name: ".$value->name." ID: ".$value->id." Link: ".$value->link."\n";
}
Question is a little light on details to debug but have you tried this?
<?php echo $campaigns[0]->name; ?>
I have this code:
$id = new matrix(array(0=>array(1,0.5,3), 1=>array(2,1,4), 2=>array(1/3,1/4,1)));
$soma = $id->times($id)->sumRows();
That outputs this:
matrix Object ( [numbers] => Array ( [0] => Array ( [0] => 12.75 [1] => 22.3333333333 [2] => 4.83333333333 ) ) [numColumns] => 3 [numRows] => 1 )
and:
$total = $id->times($id)->sumRows()->sumTotal($id);
That outputs this:
matrix Object ( [numbers] => Array ( [0] => Array ( [0] => 39.9166666667 ) ) [numColumns] => 3 [numRows] => 1 )
Now, i am trying to make:
foreach ($soma as $value){
$final = (int)$value/(int)$total;
print_r ((int)$final);
}
The output will be 000.
Must be:
12.75/39.9166666667 = 0,3269230769230769
22.3333333333 / 39.9166666667 = ...
and so on
Thanks!
Just some ideas, without really knowing much about the matrix class...
All those (int)s should probably be (float)s, as you seem to want a non-int answer.
$value is itself an object, so you'll probably need to use $value['numbers'][0][0 or 1 or 2]. Same goes for $total.
the issue is solved :
documentation:
get_data($..)
So I have some data that's coming back from a database query, and the resulting array (gotten with print_r) looks like this (it's assigned to a var called $locationData):
Array
(
[0] => Array
(
[id] => 1
[location_name] => Cook Minnesota
[location_lat] => 47.72037
[location_long] => -90.32667
)
[1] => Array
(
[id] => 2
[location_name] => Lake Minnesota
[location_lat] => 47.18238
[location_long] => -91.35301
)
[2] => Array
(
[id] => 3
[location_name] => St. Louis Minnesota
[location_lat] => 46.83572
[location_long] => -91.96299
)
)
I have a foreach loop that needs to grab the location_name from each. It looks like this:
foreach ($locationData as $location => $value ) {
echo '<p>name ' . $location['location_name']. '</p>';
}
I'm 99% certain this should work; it's basically the same code I've used a dozen times before. But the echo is not returning anything - not even the static text (<p>name). It's not throwing any errors, and if I try to do a print_r($location), I get nothing.
Any ideas? I'm sure it's something really simple.
You want $value['location_name'] because $value represents the arrays while $location represents the indexes of the arrays:
echo '<p>name ' . $value['location_name']. '</p>';
It is:
foreach ($locationData as $location => $value ) {
echo '<p>name ' . $value['location_name']. '</p>';
}