logo
down
shadow

Loop over one array as if it was a multidimensional array


Loop over one array as if it was a multidimensional array

Content Index :

Loop over one array as if it was a multidimensional array
Tag : php , By : Jakub Filak
Date : December 05 2020, 12:23 PM

it fixes the issue I have an array like: , Try this :
if(!is_array($array[0])) {
    $new_array[] = $array;
    $array = $new_array;
}

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

PHP multidimensional array - foreach loop showing last array


Tag : php , By : user183289
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Fix your syntax. echo is not inside the {}s as you intend. Otherwise, PHP will assume that you just intend to do the echo, and won't realize you want to do an internal loop.
To fix:
foreach($worksheet as $ws=>$value)
{
     echo $ws.'<br/>';

PHP: Loop through multidimensional array and establish parent-child relationships between array items


Tag : php , By : glisignoli
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I don't think you should get into objects. Plus I think it would just be extra work to generate objects and etc. In my opinion you should loop through the array and generate a multidimensional array that represents the navigational hierarchy and then loop the generated array recursively to generate your HTML. I've done a sample code for you, it works the way you want it to but you probably want to make some changes.
functions
// Generate your multidimensional array from the linear array
function GenerateNavArray($arr, $parent = 0)
{
    $pages = Array();
    foreach($arr as $page)
    {
        if($page['parent'] == $parent)
        {
            $page['sub'] = isset($page['sub']) ? $page['sub'] : GenerateNavArray($arr, $page['id']);
            $pages[] = $page;
        }
    }
    return $pages;
}

// loop the multidimensional array recursively to generate the HTML
function GenerateNavHTML($nav)
{
    $html = '';
    foreach($nav as $page)
    {
        $html .= '<ul><li>';
        $html .= '<a href="' . $page['link'] . '">' . $page['name'] . '</a>';
        $html .= GenerateNavHTML($page['sub']);
        $html .= '</li></ul>';
    }
    return $html;
}
$nav = Array
(
    Array
    (
        'id' => 27,
        'name' => 'home',
        'link' => 'home.html',
        'parent' => 0
    ),
    Array
    (
        'id' => 30,
        'name' => 'about',
        'link' => 'about.html',
        'parent' => 27
    )
);

$navarray = GenerateNavArray($nav);
echo GenerateNavHTML($navarray);

Storing a multidimensional array from another array and calling it in a foreach loop


Tag : php , By : new Blackberry devel
Date : March 29 2020, 07:55 AM
wish help you to fix your issue So i have a shopping cart creation issue, most of the code is working, just the final part of the For each loop is not be called. I believe the problem is somehow the array that it uses is being overwritten. , try this:
if(isset($_POST["submit_1"])){
  $color = $_POST['color_select'];
  $size = $_POST['size_select'];
  $price = $_POST['price'];
  $range = $_POST['range'];
  $item_array = array('i_color' => $color, 'i_size' => $size, 'i_price' => $price, 'i_range' => $range);
  $_SESSION["item"][] = $item_array;
}

Loop through one array and find value in a subarray of a different multidimensional array


Tag : php , By : lietkynes
Date : March 29 2020, 07:55 AM
this one helps. Required:
Format User Supplied Answers Indicate unanswered questions for the user
foreach ($answerArray as $id => $oneSetOfAnswers) {

    $unasweredQuestions = array();  // need a copy as it is a 'state vector'
                                    // of 'processed questions for this individual.
    foreach($questionArray as $oneQuestion) {
        $unasweredQuestions[$oneQuestion['ueID']] = $oneQuestion;
    }

    // process the answers...
    echo '<br />', '[' . $id . ']';

    foreach ($oneSetOfAnswers as $id => $oneAnswer) {


        $queId = $oneAnswer['QueID'];

        $theAnswers = getAnswer($unasweredQuestions[$queId], $oneAnswer);

        // remove from the list
        unset($unasweredQuestions[$queId]);

        // show answers
        foreach($theAnswers as $oneLine) {
            echo '<br />', $oneLine;
        }
    }

    // show the unansweredQuestions
    foreach ($unasweredQuestions as $question) {
        echo '<br />', $question['ueText'] .' : no answer supplied.';
    }
}
/**
 *
 * @param type $question
 * @param type $oneAnswer
 * @return array ready to print
 */
function getAnswer($question,  $oneAnswer)
{
    $useAnsText =    strpos(strtolower($question['ueType']), 'multi') !== false
                  || strpos(strtolower($question['ueType']), 'single') !== false;
    $q = $question['ueText'];

    if ($useAnsText) {
        return array($q . ' : '. $oneAnswer['AnsText']);
    }

    // ignore standard stuff and assume whatever remains is wanted
    $ignore = array('AnsText', 'QueID', 'QueType');

    $answer = array($q);

    foreach($oneAnswer as $id => $response) {
        if (in_array($id, $ignore)) {
            continue;
        }

        $answer[] = $id .' : '. $response;
    }
    return $answer;
}

Loop Multidimensional array to generate Multidimensional Array for Google Charts


Tag : javascript , By : Juan Pablo
Date : March 29 2020, 07:55 AM
this one helps. Edit: This uses a function and a temporary variable temp for collecting values. Every third item, a new array is pushed to the result array.
function x(data) {
    var r = [], temp;
    data.forEach(function (a, i) {
        if (!(i % 3)) {
            temp = [a[1]];
            r.push(temp);
        }
        temp.push(a[2]);
    });
    return r;
}

var data = [['ANSWERED', '477', 728], ['BUSY', '477', 48], ['NO ANSWER', '477', 277], ['ANSWERED', '478', 88], ['BUSY', '478', 24], ['NO ANSWER', '478', 56]],
    result = [['Call Disposition', 'Answered', 'Busy', 'Failed']].concat(x(data));

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
Related Posts Related QUESTIONS :
  • How to add spell check to a php function
  • Is it posible to create css rules with php?
  • Pretty URL with $_GET method
  • Wordpress always load same page - index.php
  • Find value in array and return with new from different array
  • PHP multiple file upload with different keys
  • Can't modify times one a time without weird errors
  • Get size of scraped image with domcrawler (Goutte)
  • Laravel - Ordering a collection of models
  • PHP Fatal error: Uncaught Error: Call to a member function setFetchMode() on boolean
  • Strict Standards: Only variables should be passed by reference in /main_dir/sub_dir/backup.php
  • Uncaught Google_Exception: (update) unknown parameter: 'copyRequiresWriterPermission'
  • Laravel 5 form does not redirect with old input
  • How to get values greater than or equal to in Mysql database sql when its a value like 1:00 PM?
  • PHP datediff overday issue
  • Masking an auto incrementing primary key
  • How to grab a String BEFORE a certain word
  • ZF2 Routing Multiple Controllers based on route constraints
  • How to deregister/dequeue jquery.sticky.js in Elementor (Pro)?
  • How to fix errors counting words in text plain with PHP?
  • How to get View data in Laravel in custom helper or directive
  • Creating a new data entry into a database with eloquent and trying to access the variable in the url
  • How to test authentication using Laravel Dusk?
  • PHP parse_str not getting all $_GET variables
  • Variable issues in php class
  • Get orders between dates in Codeigniter
  • Eloquent Special Chars Issue
  • Keeping leading zeros column Mysql
  • Current level navigation in Wordpress
  • Add a custom text to WooCommerce cart items if the product has a specific shipping class
  • Error connecting to CommissionJunction Token : [60] SSL certificate problem: unable to get local issuer certificate
  • How do I add a new key value pair to an object in PHP?
  • Http post request to php with dart
  • How to add a route in with blade into parameters of a component
  • How can i get the source of a 777 - CHMOD php file?
  • Convert 2010-04-16 16:30:00 to "Tomorrow Afternoon"
  • How can I hide a $_GET variable to be more secured in PHP?
  • Login to Gmail Inbox using Curl?
  • Upload Excel or CSV file to MySQL with PHP
  • Outlook Marking Email as Junk Email
  • Upload an image using class.upload.php via AJAX, display image in form using jQuery
  • mysql_connect() causes page to not display (WAMP)
  • How to limit a users uploaded image size using PHP
  • What's the difference whether or not to run PHP in safe mode?
  • Logging in to a website cURL
  • Using php to create a password system with chinese characters
  • PHP is not called properly in IE using set interval function in Javascript
  • How to read pdf, ppt, xl, doc files content into a string in php/python
  • php script for currency conversion
  • In PHP... best way to turn string representation of a folder structure into nested array
  • PayPal for Indian Rupees (INR) any solution
  • PHP Deployment to Live Server
  • Ideal Multi-Developer Lamp Stack?
  • How can I save an image from a file input field using PHP & MySQL?
  • Warning: array_multisort() [function.array-multisort]: Argument #1 is expected to be an array or a sort flag in
  • Connecting to external MySQL DB from a web server not running MySQL
  • Is it possible to add custom fields to a Drupal taxonomy term?
  • PHP 6.0 - Roadmap?
  • jQuery wait for ajax call to be over before continuing
  • checkUnique function?
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com