Two different large array - php

I have problem with two arrays:
$pole1 = array(
array("klic"=>"banan", "jmeno"=>"Banán"),
array("klic"=>"pomeranc", "jmeno"=>"Pomeranč"),
);
$pole2 = array(
array("klic"=>"banan"),
);
Now I need foreach data:
foreach ($pole1 as $key => $val){
//all data from $pole
echo $val
//and here if "klic" from $pole1 == "klic" from $pole2
if ($pole2[$key]["klic"] == $pole1["klic"])
echo "YES"; // - **not working**
}
I need to check if data from $pole1 equals data from $pole2 and write some text but I need to write all data from $pole1.

You meant that?
foreach ($pole1 as $key => $val) {
if ( isset($pole2[$key]["klic"] &&
($pole2[$key]["klic"] == $pole1[$key]["klic"]) )
echo "YES";
}

try this
foreach($pole1 as $k1 => $arrays) {
foreach($arrays as $k2 => $val) {
if($pole2[$k1][$k2] == $val) {
// $pole1[$k1][$k2] is equal to $pole2[$k1][$k2]
}
}

This will echo every entry in pole1 and check every entry in pole1 with every entry in pole2.
foreach($pole1 as $val){
echo($val);
foreach($pole2 as $val2){
if($val['klic']==$val2['klic']) echo 'YES';
}
}

Related

PHP read value form multidimensional array

its easy for sure..
i have code like this:
$indeks = 0;
foreach ($list as $k => $v)
{
$data['fname'] = $customer->firstname;
$data['lname'] = $customer->lastname;
$data['code'] = $code['code'];
$tablica[$indeks] = $data;
$indeks++;
and i want to read only 'code' value for each array.
i try:
foreach($tablica as $k => $v){
foreach ($v as $key => $value ) {
echo $value
}
}
but i get all arrays values.
when i try
foreach($tablica as $k => $v){
foreach ($v['code'] as $key => $value ) {
echo $value
}
}
i have nothing...
thx for help
You can use array_column function to get all values of column, for example:
foreach (array_column($tablica, 'code') as $value) {
echo $value;
}
I think a For loop should help
for($i=0;$i<count($tablica);$i++){
echo $tablica[$i]['code'];
}
or get all Codes into an Array
$code = array();
for($i=0;$i<count($tablica);$i++){
$code[$i] = $tablica[$i]['code'];
}
You don't need nested loops.
foreach ($tablica as $value) {
echo $value['code'];
}
DEMO

How to traverse a 3-level array for a specific keyword in PHP

I have a 3 level array. I want to display the value of a particular keyword. This keyword appears in both the 2nd level and the 3rd level. But i want to display just the 2nd level.
Any idea how to do this? I have tried the following but it is displaying out all the values of the key "name".
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($call_result, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);
foreach ($jsonIterator as $key => $val) {
if(strcmp ( $key , "name") == 0)
{
//echo '<br>';
if(strcmp($val , " ")== 1)
{
echo "$key => $val\n";
}
}
}
Any idea, how to just print out the value of the 2nd level only?
foreach ($jsonIterator as $key => $val)
{
foreach($value as $key => $value)
{
if(strcmp( $key , "name") == 1)
{
if(strcmp($val , "name") == 1)
{
echo "$key\n";
}
}
}
}
foreach($va as $k => $v){
foreach($v as $k1 => $v1){
if(strcmp($k1 , "name") == 0)
{
echo "$k1 => $v1\n";
}
}
}

php doesn't fetch end of array

foreach doesn't fetch end of array. I want to use end of that in another method:
$array = array(
"Language programings"=>array("php" => 100,"js" => 200),'html'=>12);
foreach ($array as $kk=>$val1)
{
echo $kk.'<br/>';
foreach ($val1 as $key=>$val2)
{
if (! end(array_keys($array)))
echo $val2;
}
echo end(array_value);//must be show 12
}
If I understand correctly, in your if() statement, you're attempting to see if the pointer is at the end of the array. Unfortunately, in this case, end() will never be false and so the line echo $val2; won't ever execute.
Try replacing
if (! end(array_keys($array)))
with
if ($key <> end(array_keys($array))
also your last line should be:
echo end(array_values($array));
Try using below code:
$array = array("Language programings"=>array("php" => 100,"js" => 200),'html'=>12);
foreach($array as $key1 => $val1){
if(is_array($val1)){
echo $key1.'<br/>';
foreach($val1 as $key2 => $val2){
if(is_array($val2)){
foreach($val2 as $k => $v){
echo $v.'<br/>';
}
} else {
echo $val2.'<br/>';
}
}
}
}
echo end(array_values($array));
The result will be:
Language programings
100
200
12

how do I echo out an array array using foreach?

i am trying to automate my navigation links. How do I automatically echo out foreach. I am getting undefined offset right now... And can I ignore the first item in the array (i.e. title)?
'control' => array( 0=>'Controls',
1=> array('Add school','add.school.php'),
2=> array('Add doctor','add.doctor.php'),
3=> array('Add playgroup','add.play.php'),
4=> array('Suggestions','suggestion.php'),
5=> array('List tutor service','list.tutor.php'),
6=> array('Create playgroup','create.play.php'),
7=> array('Dashboard', 'dashboard.php')
),
<?php
foreach ($nav['control'] as $value=>$key){
echo''.$key[1].'';
}
?>
Numeric arrays are indexed from 0, not 1. You want [1] and [0] respectively.
// for key => value is more nature.
foreach ($nav['control'] as $key => $value){
// should skip the first.
if ($key === 0) {
continue;
}
// array is 0 base indexed.
echo''.$value[0].'';
}
foreach ($nav['control'] as $value=>$key) {
echo''.$key[0].'';
}
a nested array requires nested loop.
foreach($array as $key => $value){
if($key != 0){
foreach($value as $k => $v){
if($k == 0){ $title = $v;}
if($k == 1){ $link = $v;}
}
//put code to run for each entry here (i.e. <div> tags, echo $title and $link)
}
my personal practice when i only use two fields is to push the first into the array['id'] and the second as the value i.e.
while($row_links = sth->fetch (PDO::FETCH_ASSOC)){
$array[$row_links['title']] = $row_links['link'];
}
then you can use
<?php foreach($array as $key => $value){ ?>
<?php echo $key; ?>
<?php } ?>

Search multidimensional arrays for specific keys and output their data

I have following array construction: $array[$certain_key][some_text_value]
And in a while loop, I want to print the data from the array, where $certain_key is a specific value.
I know how to loop through multidimensional arrays, which is not the complete solution to this problem:
foreach ($a as $v1) {
foreach ($v1 as $v2) {
echo "$v2\n";
}
}
I do not want to loop the whole array each time, but only when $certain_key is matched.
EDIT: to be more exact, this is what I'm trying to do:
$array[$array_key][some_text];
while reading from db {
//print array where a value returned from the db = $array_key
}
while ($row = fetch()) {
if (isset($array[$row['db_id']])) {
foreach ($array[$row['db_id']] as $some_text_value => $some_text_values_value) {
echo ...
}
}
}
foreach ($array as $certain_key => $value) {
if($certain_key == $row['db_id']) {
foreach ($value as $some_text_value) {
echo "$v2\n";
}
}
}
You mean like
foreach($array[$certain_key] as $k => $v)
{
do_stuff();
}
?
Maybe you're looking for array_key_exists? It works like this:
if(array_key_exists($certain_key, $array)) {
// do something
}
<?php
foreach ($a as $idx => $value) {
// replace [search_value] with whatever key you are looking for
if ('[search_value]' == $idx) {
// the key you are looking for is stored as $idx
// the row you are looking for is stored as $value
}
}

Categories