1

I got a project half-way through and I got this weird array which I now need to go through and store in a database.

The (weird) array $fields is like this:

array(10) {
    ["item_id"]=> string(1) "2" 
    ["itemname"]=> array(3) {
        [1]=> string(18) "Keyboard in en" 
        [2]=> string(21) "Keyboard in gr" 
        [3]=> string(20) "Keyboard in ru" 
    } 
    ["shortDesc"]=> array(3) {
        [1]=> string(0) "short_d en" 
        [2]=> string(0) "short_d gr" 
        [3]=> string(0) "short_d ru" 
    }
    ["description"]=> array(3) {
        [1]=> string(7) "en test" 
        [2]=> string(7) "gr test" 
        [3]=> string(7) "ru test" 
    }
    ["item_code"]=> string(12) "PH31004PCS"
}

The table structure is like this:

id(AI) | language_id | item_code | item_name | short_description  | description |

I need to automatically find the number of languages, (in the example is 3 but there could be more), and insert for each row the appropriate data.

Example: The row for English and Russian would be like this:

| id(AI) | language_id | item_code  | item_name      | short_description  | description |
+--------+-------------+------------+----------------+--------------------+-------------+
| 1      | 1           | PH31004PCS | Keyboard in en | short_d en         | en test     |
| 2      | 3           | PH31004PCS | Keyboard in ru | short_d ru         | ru test     |

The problem: How should I navigate the array so I can store each row correctly and for all languages.


Failed attempts

However I tried to navigate the array all I managed to do was to get each inner array individually, all the names then all the short descriptions and then all the descriptions, instead of the first then second for each inner array and so on.

2 Answers 2

2

You could just use for in this case. But first you need to prepare the values.

$db = new mysqli('localhost', 'username', 'password', 'database');

// prepare values
$num_rows = count($array['itemname']); // get the number of rows of array
$item_id = $array['item_id']; // get the item id
$item_code = $array['item_code']; // get itemcode
unset($array['item_id'], $array['item_code']); // unset them, you don't need them when you loop later

// as usual, prepare
$insert = $db->prepare('
    INSERT INTO table_name (language_id, item_code, item_name, short_description, description)
    VALUES (?, ?, ?, ?, ?)
');

// start zero or 1 of $i
for($i = 1; $i <= $num_rows; $i++) {
    // you need to figure out how to get the language id, might be another select here

    $insert->bind_param('issss', $language_id, $item_code, $array['itemname'][$i], $array['shortDesc'][$i], $array['description'][$i]);
    $insert->execute();
}

This is just the basic idea. Most likely you didn't post all of the contents since it looks like you have more data inside the array array(10).

Sign up to request clarification or add additional context in comments.

1 Comment

"Most likely you didn't post all of the contents" Yep just the important ones :D, Although its very similar to @Deer-Outdoor.nl's anwer, I chose yours as the correct because of the extra information with the database :)
1
$count = count($array['item_id']);

for($i = 0; $i < $count; $i++){
    $lang_id = $array['item_id'];
    $item_code = $array['item_code'];
    $item_name = $array['itemname'][$i];
    $short_desc = $array['shortDesc'][$i];
    $desc = $array['description'][$i];

    $sql = "INSERT INTO `your_table_name_here` (`language_id`, `item_code`, `item_name`, `short_description`, `description`) VALUES ('$lang_id', '$item_code', '$item_name','$short_desc','$desc')";
    //dont forget to fire ur sql at the database!!
}

Something like this maybe?

2 Comments

Yep exactly like this :D, the reason I chose the other answer as the correct is only because of the extra info for the database. Thank you for your time :)
Okay np. glad i could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.